隨筆 - 5, 文章 - 0, 評論 - 0, 引用 - 0
          數據加載中……

          gc 對象晉升old區的一些規則總結

          ygc做步驟為:清空eden+from中所有no ref的對象占用的內存
          將eden+from中的所有存活的對象copy到to中
          在這個過程中一些對象將晉升到old中:
          --to放不下的
          --存活次數超過tenuring threshold的
          重新計算Tenuring Threshold

          然后談談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 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,并對其所占用的大小進行累積,當累積的大 小超過了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后均會動態計算,規則比上面的復雜,在設置-XX:-UseAdaptiveSizePolicy后,則以 MaxTenuringThrehsold為準,并且不會重新計算,會是恒定值。

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

          posted on 2012-10-23 20:52 莊主游驥 閱讀(849) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 高阳县| 临夏县| 扎囊县| 南郑县| 合作市| 攀枝花市| 鄂托克旗| 青冈县| 镇原县| 建阳市| 博乐市| 长子县| 正镶白旗| 德保县| 博湖县| 卓尼县| 渝北区| 邻水| 邓州市| 余干县| 桐乡市| 浙江省| 子洲县| 常宁市| 平顶山市| 定陶县| 金平| 绥棱县| 江津市| 离岛区| 修武县| 新巴尔虎左旗| 余庆县| 霸州市| 龙口市| 叙永县| 泽库县| 灵石县| 抚顺县| 曲阳县| 山西省|