JVM堆中的配置参数

mac2024-03-12  24

堆内存特性

1)、新生代内存 = eden区内存 + from区内存 +to区内存 ,from区内存 = to区内存;

2)、新生代可用内存 = eden区内存 + from区内存(复制算法需要预留一个survivor区);

3)、堆空间内存 = 新生代内存 + 老年代内存;

4)、默认情况下,eden区内存:from区内存:to区内存 = 8:1:1;

5)、默认情况下,新生代内存:老年代内存=1:2。

堆内存配置参数

1)、-XX:NewSize:设置新生代的初始内存大小;-XX:MaxNewSize:设置新生代最大内存大小;

2)、-Xmn,设置新生代的内存大小,初始内存大小=最大内存大小;

3)、-XX:SurvivorRatio:1个eden区与1个survivor区的比例,默认为8;

4)、-XX:NewRatio:老年代内存与新生代的比例,默认为2。

举例说明堆内存特性以及多个堆内存配置参数的同时使用的情况

【设置JVM参数】,设置堆空间大小为40M

package com.su.mybatis.oracle.controller; public class Test { public static void main(String[] args) { String s = new String("123"); } }

1、堆内存特性,-Xmx40M -Xms40M -XX:+PrintGCDetails

输出结果:

Heap def new generation total 12288K, used 876K [0x31d00000, 0x32a50000, 0x32a50000) eden space 10944K, 8% used [0x31d00000, 0x31ddb070, 0x327b0000) from space 1344K, 0% used [0x327b0000, 0x327b0000, 0x32900000) to space 1344K, 0% used [0x32900000, 0x32900000, 0x32a50000) tenured generation total 27328K, used 0K [0x32a50000, 0x34500000, 0x34500000) the space 27328K, 0% used [0x32a50000, 0x32a50000, 0x32a50200, 0x34500000) compacting perm gen total 12288K, used 153K [0x34500000, 0x35100000, 0x38500000) the space 12288K, 1% used [0x34500000, 0x34526780, 0x34526800, 0x35100000) ro space 10240K, 44% used [0x38500000, 0x38977808, 0x38977a00, 0x38f00000) rw space 12288K, 52% used [0x38f00000, 0x3954e610, 0x3954e800, 0x39b00000)

2、-XX:NewSize和-XX:MaxNewSize的使用,-Xmx40M -Xms40M -XX:NewSize=10M -XX:MaxNewSize=10M -XX:+PrintGCDetails

输出结果:

Heap def new generation total 9216K, used 876K [0x31d00000, 0x32700000, 0x32700000) eden space 8192K, 10% used [0x31d00000, 0x31ddb148, 0x32500000) from space 1024K, 0% used [0x32500000, 0x32500000, 0x32600000) to space 1024K, 0% used [0x32600000, 0x32600000, 0x32700000) tenured generation total 30720K, used 0K [0x32700000, 0x34500000, 0x34500000) the space 30720K, 0% used [0x32700000, 0x32700000, 0x32700200, 0x34500000) compacting perm gen total 12288K, used 153K [0x34500000, 0x35100000, 0x38500000) the space 12288K, 1% used [0x34500000, 0x34526780, 0x34526800, 0x35100000) ro space 10240K, 44% used [0x38500000, 0x38977808, 0x38977a00, 0x38f00000) rw space 12288K, 52% used [0x38f00000, 0x3954e610, 0x3954e800, 0x39b00000)

3、-Xmn的使用,-Xmx40M -Xms40M -XX:+PrintGCDetails -Xmn5M

输出结果:

Heap def new generation total 4608K, used 776K [0x31d00000, 0x32200000, 0x32200000) eden space 4096K, 18% used [0x31d00000, 0x31dc2310, 0x32100000) from space 512K, 0% used [0x32100000, 0x32100000, 0x32180000) to space 512K, 0% used [0x32180000, 0x32180000, 0x32200000) tenured generation total 35840K, used 0K [0x32200000, 0x34500000, 0x34500000) the space 35840K, 0% used [0x32200000, 0x32200000, 0x32200200, 0x34500000) compacting perm gen total 12288K, used 153K [0x34500000, 0x35100000, 0x38500000) the space 12288K, 1% used [0x34500000, 0x34526780, 0x34526800, 0x35100000) ro space 10240K, 44% used [0x38500000, 0x38977808, 0x38977a00, 0x38f00000) rw space 12288K, 52% used [0x38f00000, 0x3954e610, 0x3954e800, 0x39b00000)

4、-XX:SurvivorRatio的使用,-Xmx40M -Xms40M -XX:+PrintGCDetails -XX:SurvivorRatio=4

输出结果:

Heap def new generation total 11392K, used 987K [0x31d00000, 0x32a50000, 0x32a50000) eden space 9152K, 10% used [0x31d00000, 0x31df6f90, 0x325f0000) from space 2240K, 0% used [0x325f0000, 0x325f0000, 0x32820000) to space 2240K, 0% used [0x32820000, 0x32820000, 0x32a50000) tenured generation total 27328K, used 0K [0x32a50000, 0x34500000, 0x34500000) the space 27328K, 0% used [0x32a50000, 0x32a50000, 0x32a50200, 0x34500000) compacting perm gen total 12288K, used 153K [0x34500000, 0x35100000, 0x38500000) the space 12288K, 1% used [0x34500000, 0x34526780, 0x34526800, 0x35100000) ro space 10240K, 44% used [0x38500000, 0x38977808, 0x38977a00, 0x38f00000) rw space 12288K, 52% used [0x38f00000, 0x3954e610, 0x3954e800, 0x39b00000)

5、-XX:NewRatio的使用,-Xmx40M -Xms40M -XX:+PrintGCDetails -XX:NewRatio=4

输出结果:

Heap def new generation total 7424K, used 803K [0x31d00000, 0x32500000, 0x32500000) eden space 6656K, 12% used [0x31d00000, 0x31dc8c30, 0x32380000) from space 768K, 0% used [0x32380000, 0x32380000, 0x32440000) to space 768K, 0% used [0x32440000, 0x32440000, 0x32500000) tenured generation total 32768K, used 0K [0x32500000, 0x34500000, 0x34500000) the space 32768K, 0% used [0x32500000, 0x32500000, 0x32500200, 0x34500000) compacting perm gen total 12288K, used 153K [0x34500000, 0x35100000, 0x38500000) the space 12288K, 1% used [0x34500000, 0x34526728, 0x34526800, 0x35100000) ro space 10240K, 44% used [0x38500000, 0x38977808, 0x38977a00, 0x38f00000) rw space 12288K, 52% used [0x38f00000, 0x3954e610, 0x3954e800, 0x39b00000)

6、-XX:NewSize/-XX:MaxNewSize与-Xmn同时使用,且-XX:NewSize/-XX:MaxNewSize在前,-Xmx40M -Xms40M -XX:NewSize=10M -XX:MaxNewSize=10M -XX:+PrintGCDetails   -Xmn5M

输出结果:

Heap def new generation total 4608K, used 776K [0x31d00000, 0x32200000, 0x32200000) eden space 4096K, 18% used [0x31d00000, 0x31dc2310, 0x32100000) from space 512K, 0% used [0x32100000, 0x32100000, 0x32180000) to space 512K, 0% used [0x32180000, 0x32180000, 0x32200000) tenured generation total 35840K, used 0K [0x32200000, 0x34500000, 0x34500000) the space 35840K, 0% used [0x32200000, 0x32200000, 0x32200200, 0x34500000) compacting perm gen total 12288K, used 153K [0x34500000, 0x35100000, 0x38500000) the space 12288K, 1% used [0x34500000, 0x34526728, 0x34526800, 0x35100000) ro space 10240K, 44% used [0x38500000, 0x38977808, 0x38977a00, 0x38f00000) rw space 12288K, 52% used [0x38f00000, 0x3954e610, 0x3954e800, 0x39b00000)

7、-XX:NewSize/-XX:MaxNewSize与-Xmn同时使用,且-Xmn在前,-Xmx40M -Xms40M -XX:NewSize=10M -XX:MaxNewSize=10M -XX:+PrintGCDetails   -Xmn5M

输出结果:

Heap def new generation total 9216K, used 876K [0x31d00000, 0x32700000, 0x32700000) eden space 8192K, 10% used [0x31d00000, 0x31ddb148, 0x32500000) from space 1024K, 0% used [0x32500000, 0x32500000, 0x32600000) to space 1024K, 0% used [0x32600000, 0x32600000, 0x32700000) tenured generation total 30720K, used 0K [0x32700000, 0x34500000, 0x34500000) the space 30720K, 0% used [0x32700000, 0x32700000, 0x32700200, 0x34500000) compacting perm gen total 12288K, used 153K [0x34500000, 0x35100000, 0x38500000) the space 12288K, 1% used [0x34500000, 0x34526728, 0x34526800, 0x35100000) ro space 10240K, 44% used [0x38500000, 0x38977808, 0x38977a00, 0x38f00000) rw space 12288K, 52% used [0x38f00000, 0x3954e610, 0x3954e800, 0x39b00000)

6与7对比得出结论:-XX:NewSize/-XX:MaxNewSize与-Xmn同时存在时,后面的配置生效,或者理解为后面的配置会覆盖前面的配置。

8、-Xmn与-XX:SurvivorRatio同时使用,不会冲突,都会生效,-Xmx40M -Xms40M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=4

输出结果:

Heap def new generation total 8576K, used 854K [0x31d00000, 0x32700000, 0x32700000) eden space 6912K, 12% used [0x31d00000, 0x31dd5940, 0x323c0000) from space 1664K, 0% used [0x323c0000, 0x323c0000, 0x32560000) to space 1664K, 0% used [0x32560000, 0x32560000, 0x32700000) tenured generation total 30720K, used 0K [0x32700000, 0x34500000, 0x34500000) the space 30720K, 0% used [0x32700000, 0x32700000, 0x32700200, 0x34500000) compacting perm gen total 12288K, used 153K [0x34500000, 0x35100000, 0x38500000) the space 12288K, 1% used [0x34500000, 0x34526728, 0x34526800, 0x35100000) ro space 10240K, 44% used [0x38500000, 0x38977808, 0x38977a00, 0x38f00000) rw space 12288K, 52% used [0x38f00000, 0x3954e610, 0x3954e800, 0x39b00000)

9、堆空间内存固定(-Xmx、-Xms),新生代大小固定(-Xmn或者-XX:NewSize/-XX:MaxNewSize),-XX:NewRatio不会生效,与配置参数顺序无关

1)、-Xmx40M -Xms40M  -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=4 -XX:NewRatio=8

2)、-Xmx40M -Xms40M  -XX:+PrintGCDetails -XX:SurvivorRatio=4 -XX:NewRatio=8  -Xmn10M

3)、-Xmx40M -Xms40M -XX:NewSize=10M -XX:MaxNewSize=10M -XX:+PrintGCDetails -XX:SurvivorRatio=4 -XX:NewRatio=8

4)、-Xmx40M -Xms40M  -XX:+PrintGCDetails -XX:SurvivorRatio=4 -XX:NewRatio=8  -XX:NewSize=10M -XX:MaxNewSize=10M

输出结果(相同):

Heap def new generation total 8576K, used 854K [0x31d00000, 0x32700000, 0x32700000) eden space 6912K, 12% used [0x31d00000, 0x31dd5940, 0x323c0000) from space 1664K, 0% used [0x323c0000, 0x323c0000, 0x32560000) to space 1664K, 0% used [0x32560000, 0x32560000, 0x32700000) tenured generation total 30720K, used 0K [0x32700000, 0x34500000, 0x34500000) the space 30720K, 0% used [0x32700000, 0x32700000, 0x32700200, 0x34500000) compacting perm gen total 12288K, used 153K [0x34500000, 0x35100000, 0x38500000) the space 12288K, 1% used [0x34500000, 0x34526728, 0x34526800, 0x35100000) ro space 10240K, 44% used [0x38500000, 0x38977808, 0x38977a00, 0x38f00000) rw space 12288K, 52% used [0x38f00000, 0x3954e610, 0x3954e800, 0x39b00000)

 

 

如果有写的不对的地方,请大家多多批评指正,非常感谢!

最新回复(0)