您的位置:首页 > 其它

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

2017-11-07 13:55 197 查看
为了能更好地适应不同程序的内存状况,虚拟机并不是永远地要求对象的年龄必须达到了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 [DefNew
Desired 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 [DefNew
Desired 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 generati
4000
on 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操作,就会发现另外一个就不会晋升到老年代中去了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: