ygc做步驟為:清空eden+from中所有no ref的對象占用的內(nèi)存
將eden+from中的所有存活的對象copy到to中
在這個過程中一些對象將晉升到old中:
--to放不下的
--存活次數(shù)超過tenuring threshold的
重新計算Tenuring Threshold
然后談?wù)凪axTenuringThreshold這個參數(shù)用于控制對象能經(jīng)歷多少次Minor GC才晉升到舊生代,默認值是15,那是不是意味著對象要經(jīng)歷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參數(shù)執(zhí)行以上代碼,通過
jstat -gcutil [pid] 1000 10的方式查看執(zhí)行效果,很驚訝執(zhí)行結(jié)果竟然是在第二次minor GC的時候object1就被晉升到old中了,而可以肯定的是這個時候
to space空間是充足的,也就是說并不是在to space空間充足的情況下,對象一定要經(jīng)歷MaxTenuringThreshold次才會晉升到old,那具體規(guī)則到底是怎么樣的呢,翻看 Hotspot 6 update 21中SerialGC的實現(xiàn),可以看到在每次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 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
其中desired_survivor_size是指survivor space/2,從上面的代碼可看出,在計算存活周期這個閾值時,
hotspot會遍歷所有age的table,并對其所占用的大小進行累積,當(dāng)累積的大 小超過了survivor space的一半時,則以這個age作為新的存活周期閾值,最后取age和MaxTenuringThreshold中更小的一個值。按照這樣的規(guī)則,上面的運行效果就可驗證了,第一次minor gc的時候存活周期的閾值為MaxTenuringThreshold,minor gc結(jié)束后計算出新的閾值為1,在第二次minor gc時object 1的age已經(jīng)是1了,因此object1被晉升到了舊生代。
這個規(guī)則對于Serial GC以及ParNew GC(但對于開啟了UseAdaptiveSizePolicy的ParNew GC而言也無效,默認是不開啟的)均有效,對于PS(Parallel Scavenge) GC而言,在默認的情況下第一次以InitialTenuringThreshold(默認為7)為準(zhǔn),之后在每次minor GC后均會動態(tài)計算,規(guī)則比上面的復(fù)雜,在設(shè)置-XX:-UseAdaptiveSizePolicy后,則以 MaxTenuringThrehsold為準(zhǔn),并且不會重新計算,會是恒定值。
如希望跟蹤每次minor GC后新的存活周期的閾值,可在啟動參數(shù)上增加:-XX:+PrintTenuringDistribution,輸出的信息中的:
Desired survivor size 1048576 bytes, new threshold 7 (max 15)
new threshold 7即標(biāo)識新的存活周期的閾值為7。