JVM调优参数

本文介绍JVM调优常见参数

XX参数

1
-XX:SurvivorRatio=n:年轻代中Eden区与两个Survivor区的比值。默认-XX:SurvivorRatio=8, 即Eden:S0:S1 = 8:1:1

我们设置JVM参数为:-XX:+PrintGCDetails -XX:+UseSerialGC -Xms10m -Xmx10m

1
2
3
4
5
 ...
eden space 2752K,
from space 320K,
to space 320K,
...

从输出信息可以看出,eden space 2752K from space 320K to space 320K ,320*8≈2752,8:1:1并不是非常准确的比例

我们再把JVM参数设为:-XX:+PrintGCDetails -XX:+UseSerialGC -Xms10m -Xmx10m -XX:SurvivorRatio=4

1
2
3
4
5
...
eden space 2368K,
from space 512K,
to space 512K,
...

可以看出大致是4:1:1的关系

1
-XX:NewRatio 配置年轻代与老年代在堆结构的占比,默认为2,新生代占1老年代占2

设置JVM参数为:-XX:+PrintGCDetails -XX:+UseSerialGC -Xms10m -Xmx10m -XX:NewRatio=2

1
2
new generation   total 3072K
tenured generation total 6848K,

可以看出年轻代和老年代的比例大致为1:2

再把参数改为-XX:+PrintGCDetails -XX:+UseSerialGC -Xms10m -Xmx10m -XX:NewRatio=4

1
2
new generation   total 1856K
tenured generation total 8192K
1
-XX:MaxTenuringThreshold 设置垃圾最大年龄,默认15

设置JVM参数为:-XX:MaxTenuringThreshold =20,会输出一下信息

1
2
3
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
MaxTenuringThreshold of 20 is invalid; must be between 0 and 15
0%