程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 說說MaxTenuringThreshold這個參數

說說MaxTenuringThreshold這個參數

編輯:關於JAVA
 

MaxTenuringThreshold這個參數用於控制對象能經歷多少次Minor GC才晉升到舊生代,默認值是15,那是不是意味著對象要經歷15次minor gc才晉升到舊生代呢,來看下面的一個例子。

public class GCTenuringThreshold{
public static void main(String[] args) throws Exception{
GCMemoryObject object1=new GCMemoryObject(2);
GCMemoryObject object2=new GCMemoryObject(8);
GCMemoryObject object3=new GCMemoryObject(8);
GCMemoryObject object4=new GCMemoryObject(8);
object2=null;
object3=null;
GCMemoryObject object5=new GCMemoryObject(8);
Thread.sleep(4000);
object2=new GCMemoryObject(8);
object3=new GCMemoryObject(8);
object2=null;
object3=null;
object5=null;
GCMemoryObject object6=new GCMemoryObject(8);
Thread.sleep(5000);
}
}
class GCMemoryObject{
private byte[] bytes=null;
public GCMemoryObject(int multi){
bytes=new byte[1024*256*multi];
}
}

以-Xms20M –Xmx20M –Xmn10M –XX:+UseSerialGC參數執行以上代碼,通過jstat -gcutil [pid] 1000 10的方式查看執行效果,很驚訝執行結果竟然是在第二次minor GC的時候object1就被晉升到old中了,而可以肯定的是這個時候to space空間是充足的,也就是說並不是在to space空間充足的情況下,對象一定要經歷MaxTenuringThreshold次才會晉升到old,那具體規則到底是怎麼樣的呢,翻看Hotspot 6 update 21中SerialGC的實現,可以看到在每次minor GC後,會對這個存活周期的阈值做計算,計算的代碼如下:

size_t desired_survivor_size = (size_t)((((double) survivor_capacity)*TargetSurvivorRatio)/100);
size_t total = 0;
int age = 1;
assert(sizes[0] == 0, "no objects with age zero should be recorded");
while (age < table_size) {
total += sizes[age];
// check if including objects of age 'age' made us pass the desired
// size, if so 'age' is the new threshold
if (total > desired_survivor_size) break;
age++;
}
int result = age < MaxTenuringThreshold ? age : MaxTenuringThreshold;

其中desired_survivor_size是指survivor space/2,從上面的代碼可看出,在計算存活周期這個阈值時,hotspot會遍歷所有age的table,並對其所占用的大小進行累積,當累積的大小超過了survivor space的一半時,則以這個age作為新的存活周期阈值,最後取age和MaxTenuringThreshold中更小的一個值。

按照這樣的規則,上面的運行效果就可驗證了,第一次minor gc的時候存活周期的阈值為MaxTenuringThreshold,minor gc結束後計算出新的阈值為1,在第二次minor gc時object 1的age已經是1了,因此object1被晉升到了舊生代。

這個規則對於Serial GC以及ParNew GC(但對於開啟了UseAdaptiveSizePolicy的ParNew GC而言也無效,默認是不開啟的)均有效,對於PS(Parallel Scavenge) GC而言,在默認的情況下第一次以InitialTenuringThreshold(默認為7)為准,之後在每次minor GC後均會動態計算,規則比上面的復雜,後續再另寫一篇blog來說,在設置-XX:-UseAdaptiveSizePolicy後,則以MaxTenuringThrehsold為准,並且不會重新計算,會是恆定值。

如希望跟蹤每次minor GC後新的存活周期的阈值,可在啟動參數上增加:-XX:+PrintTenuringDistribution,輸出的信息中的:
Desired survivor size 1048576 bytes, new threshold 7 (max 15)
new threshold 7即標識新的存活周期的阈值為7。
 

 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved