最近在阅读<深入理解JVM>,其中在长期存活对象进入年老代的章节中,代码运行结果与书中不符,具体演示代码如下(jdk1.7.0_79)
1)当-XX:MaxTenuringThreshold=1时,结果符合预期,allocation1对象在第二次gc时被移至年老代.结果如图所示:
2)当-XX:MaxTenuringThreshold=15时,结果不符合预期,allocation1对象在第二次gc时仍然被移至年老代中,按理说年龄未达到15,不是应该存在于年轻代中吗?结果如图所示:
望大神指教,谢谢!!!
代码:
/**
* -XX:+UseSerialGC -verbose:gc -XX:+PrintGCDetails
* -Xms20M -Xmx20M -Xmn10M -XX:SurvivorRatio=8 -XX:MaxTenuringThreshold=1
* -XX:+UseSerialGC -verbose:gc -XX:+PrintGCDetails -Xms20M -Xmx20M -Xmn10M
* -XX:SurvivorRatio=8 -XX:MaxTenuringThreshold=1
*/
public static void testTenuringThreshold() {
byte[] allocation1, allocation2, allocation3;
allocation1 = new byte[_1MB / 4];
allocation2 = new byte[4 * _1MB];
allocation3 = new byte[4 * _1MB];
allocation3 = null;
allocation3 = new byte[4 * _1MB];
}
public static void main(String[] args) {
testTenuringThreshold();
}