【JVM】11_动态对象年龄判定

来源:互联网 发布:怪物猎人ol优化内存 编辑:程序博客网 时间:2024/05/20 09:26

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

/** * VM参数 : * -verbose:gc * -Xms20M * -Xmx20M * -Xmn10M * -XX:SurvivorRatio=8 * -XX:+PrintGCDetails * -XX:+UseSerialGC * -XX:MaxTenuringThreshold=15 * -XX:+PrintTenuringDistribution */public class Main {    private static final int _1MB = 1024 * 1024;    public static void main(String[] args) {        byte[] b1, b2, b3, b4;        b1 = new byte[_1MB / 4];    //256K        b2 = new byte[_1MB / 4];    //256K        b3 = new byte[4 * _1MB];        b4 = new byte[4 * _1MB];        b4 = null;        b4 = new byte[4 * _1MB];    }}

运行结果:

[GC [DefNewDesired survivor size 524288 bytes, new threshold 1 (max 15)- age   1:     718424 bytes,     718424 total: 5491K->701K(9216K), 0.0028433 secs] 5491K->4797K(19456K), 0.0028685 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] [GC [DefNewDesired survivor size 524288 bytes, new threshold 15 (max 15)- age   1:        232 bytes,        232 total: 5209K->0K(9216K), 0.0005955 secs] 9305K->4797K(19456K), 0.0006094 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] Heap def new generation   total 9216K, used 4234K [0x00000000f9a00000, 0x00000000fa400000, 0x00000000fa400000)  eden space 8192K,  51% used [0x00000000f9a00000, 0x00000000f9e227e8, 0x00000000fa200000)  from space 1024K,   0% used [0x00000000fa200000, 0x00000000fa2000e8, 0x00000000fa300000)  to   space 1024K,   0% used [0x00000000fa300000, 0x00000000fa300000, 0x00000000fa400000) tenured generation   total 10240K, used 4797K [0x00000000fa400000, 0x00000000fae00000, 0x00000000fae00000)   the space 10240K,  46% used [0x00000000fa400000, 0x00000000fa8af558, 0x00000000fa8af600, 0x00000000fae00000) compacting perm gen  total 21248K, used 3460K [0x00000000fae00000, 0x00000000fc2c0000, 0x0000000100000000)   the space 21248K,  16% used [0x00000000fae00000, 0x00000000fb161418, 0x00000000fb161600, 0x00000000fc2c0000)No shared spaces configured.

结果分析:

注意:以上测试时在JDK1.6环境下运行的,不知为什么1.6以上的版本没有此效果(个人猜想可能是GC策略变了)

我们设置了MaxTenuringThreshold=15,会发现运行结果中Survivor的空间占用仍然为0%,而老年代比预期增加了6%,也就是说,b1、b2对象都直接进入了老年代,而没有等到15岁的临界年龄。因为这两个对象加起来已经到达了512KB,并且它们是同年的,满足同年对象达到Survivor空间的一半规则。我们只要注释掉其中一个对象new操作,就会发现另外一个就不会晋升到老年代中去了


阅读全文
0 0