内存分配与回收策略(3)

  如上中的日志信息中,第一个红色字体(5188K->720K(9216K))说明将allocation1放入了Survivor区,第二个红色字体(5925K->0K)说明将新生代内存清空,其中allocation3对象的值为null,直接清除内存,而处于Survivor区中的allocation1直接移入老年代。最后堆中内存分配情况为allocation1和allocation2都在老年代,而allocation3在新生代中。大家可自行调大-XX:MaxTenuringThreshold的值,再次观察日志信息。

动态对象年龄判定

  为了能更好地适应不同程序的内存状况,虚拟机并不是永远地要求对象的年龄必须到达MaxTenuringThreshold才能晋升到老年代,如果Survivor空间中相同年龄对象大小的总和大于Survivor空间的一半,年龄大于等于该年龄的对象就可以直接进入老年代,无需等到MaxTenuringThreshold中要求的年龄。

public class Main { private static final int _1MB = 1024 * 1024; public static void main(String[] args) { /** * VM参数:-verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8 -XX:MaxTenuringThreshold=15 -XX:+PrintTenuringDistribution -XX:+UseSerialGC * -Xms20M 设置堆大小为20M -Xmx20M 避免堆自动扩展 -Xmn10M 设置年轻代大小 * -XX:+PrintGCDetails 打印日志信息 * -XX:SurvivorRatio=8 设置Eden和Survivor大小比值 */ // TODO Auto-generated method stub byte[] allocation1, allocation2, allocation3, allocation4; allocation1 = new byte[_1MB / 4]; allocation2 = new byte[_1MB / 4]; allocation3 = new byte[4 * _1MB]; allocation4 = new byte[4 * _1MB]; //Minor GC 将allocation1和allocation2放入Survivor,将allocation3放入老年代,allocation4放入新生代 allocation4 = null; allocation4 = new byte[4 * _1MB]; } } //打印日志 [GC[DefNew Desired survivor size 524288 bytes, new threshold 1 (max 15) - age 1: 1000064 bytes, 1000064 total : 5280K->976K(9216K), 0.0055251 secs] 5280K->5072K(19456K), 0.0055829 secs] [Times: user=0.00 sys=0.02, real=0.02 secs] [GC[DefNew Desired survivor size 524288 bytes, new threshold 15 (max 15) - age 1: 136 bytes, 136 total : 5236K->0K(9216K), 0.0017304 secs] 9332K->5072K(19456K), 0.0017616 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] Heap def new generation total 9216K, used 4260K [0x00000000f9a00000, 0x00000000fa400000, 0x00000000fa400000) eden space 8192K, 52% used [0x00000000f9a00000, 0x00000000f9e28fd0, 0x00000000fa200000) from space 1024K, 0% used [0x00000000fa200000, 0x00000000fa200088, 0x00000000fa300000) to space 1024K, 0% used [0x00000000fa300000, 0x00000000fa300000, 0x00000000fa400000) tenured generation total 10240K, used 5072K [0x00000000fa400000, 0x00000000fae00000, 0x00000000fae00000) the space 10240K, 49% used [0x00000000fa400000, 0x00000000fa8f4060, 0x00000000fa8f4200, 0x00000000fae00000) compacting perm gen total 21248K, used 2558K [0x00000000fae00000, 0x00000000fc2c0000, 0x0000000100000000) the space 21248K, 12% used [0x00000000fae00000, 0x00000000fb07fa10, 0x00000000fb07fc00, 0x00000000fc2c0000) No shared spaces configured.

空间分配担保

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/82452a55031a3598c2dd936ec4576dcc.html