JVM調優YoungGC


先上代碼:

主函數:

  1.  
    public class GCDemo {
  2.  
     
  3.  
    public static void main(String[] args) throws InterruptedException{
  4.  
     
  5.  
     
  6.  
    List<GCDataObject> list = new ArrayList<GCDataObject>();
  7.  
    for(int i = 0 ; i < 4 ; i++){
  8.  
    list.add( new GCDataObject(2));
  9.  
    }
  10.  
     
  11.  
    //表示第二次fullGC,或者MinorGC可以把他回收!
  12.  
    list = null;
  13.  
     
  14.  
     
  15.  
     
  16.  
    List<GCDataObject> list2 = new ArrayList<GCDataObject>();
  17.  
    for(int i = 0 ; i < 3 ; i++){
  18.  
    list2.add( new GCDataObject(2));
  19.  
    }
  20.  
    list2 = null;
  21.  
     
  22.  
     
  23.  
    }
  24.  
    }

對象(好像直接內部類不好~):

  1.  
    public class GCDataObject {
  2.  
    byte[] bytes = null;
  3.  
    public GCDataObject(int i){
  4.  
    bytes = new byte[i *1024 * 1024];
  5.  
    }
  6.  
    }

為調優的前的參數:

  1.  
    -Xms20M -Xmx20M -Xmn10M -verbose:gc -XX:+PrintGCDetails -XX:+UseSerialGC
  2.  
     
  3.  
    打印的日志:
  4.  
    [GC (Allocation Failure) [DefNew: 7299K->524K(9216K), 0.0033509 secs] 7299K->6668K(19456K), 0.0033976 secs] [Times: user=0.02 sys=0.00, real=0.00 secs] 
  5.  
    [GC (Allocation Failure) [DefNew: 6826K->6826K(9216K), 0.0000107 secs][Tenured: 6144K->4618K(10240K), 0.0025299 secs] 12970K->4618K(19456K), [Metaspace: 2576K->2576K(1056768K)], 0.0025761 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
  6.  
    Heap
  7.  
     def new generation   total 9216K, used 2130K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
  8.  
      eden space 8192K,  26% used [0x00000000fec00000, 0x00000000fee14930, 0x00000000ff400000)
  9.  
      from space 1024K,   0% used [0x00000000ff500000, 0x00000000ff500000, 0x00000000ff600000)
  10.  
      to   space 1024K,   0% used [0x00000000ff400000, 0x00000000ff400000, 0x00000000ff500000)
  11.  
     tenured generation   total 10240K, used 4618K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
  12.  
       the space 10240K,  45% used [0x00000000ff600000, 0x00000000ffa82990, 0x00000000ffa82a00, 0x0000000100000000)
  13.  
     Metaspace       used 2582K, capacity 4486K, committed 4864K, reserved 1056768K
  14.  
      class space    used 287K, capacity 386K, committed 512K, reserved 1048576K
  15.  
     
  16.  
    大致的過程:
  17.  
    首先向虛擬機分配的4個2M的對象,當中因為參數的設置,新生代Eden為8M,兩個Survivor為1M,中間發生了1次MinorGC,是以Eden空間不足導致,而且都是立馬從Survivor再次發生MinorGC進入到了舊生代,第一次FullGC清理掉了之前的8M,此時內存分配情況為新生代的Eden為2M(有誤差),舊生代為4M,
  18.  
     
  19.  
    解決方法:
  20.  
     
  21.  
    因為FullGC非常的耗時,需要盡量減少它的次數。但是又不得不MinorGC,所以加大年輕代的空間,在撐到第一個集合的對象分配完后,在下一次分配集合后發生MinonrGC就把 之前的對象回收掉。
  22.  
    同樣利用上面的代碼(業務邏輯不變),修改的參數為:
  23.  
     
  24.  
    -Xms20M -Xmx20M -Xmn16M -verbose:gc -XX:+PrintGCDetails -XX:+UseSerialGC
  25.  
     
  26.  
    [GC (Allocation Failure) [DefNew: 11615K->523K(14784K), 0.0026278 secs] 11615K->2571K(18880K), 0.0026997 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
  27.  
    Heap
  28.  
    def new generation total 14784K, used 5006K [0x00000000fec00000, 0x00000000ffc00000, 0x00000000ffc00000)
  29.  
    eden space 13184K, 34% used [0x00000000fec00000, 0x00000000ff060bc0, 0x00000000ff8e0000)
  30.  
    from space 1600K, 32% used [0x00000000ffa70000, 0x00000000ffaf2f20, 0x00000000ffc00000)
  31.  
    to space 1600K, 0% used [0x00000000ff8e0000, 0x00000000ff8e0000, 0x00000000ffa70000)
  32.  
    tenured generation total 4096K, used 2048K [0x00000000ffc00000, 0x0000000100000000, 0x0000000100000000)
  33.  
    the space 4096K, 50% used [0x00000000ffc00000, 0x00000000ffe00010, 0x00000000ffe00200, 0x0000000100000000)
  34.  
    Metaspace used 2582K, capacity 4486K, committed 4864K, reserved 1056768K
  35.  
    class space used 287K, capacity 386K, committed 512K, reserved 1048576K
  36.  
     
  37.  
    從之前的一次MinorGC和一次FullGC減少為一次MinorGC。
  38.  
     
  39.  
    大致過程:
  40.  
    因為調整了年輕代大小,為16M,Eden為12.8M,Survivor為1.6M,第一次的集合分配,整整8M分配進了Eden,在下次集合的分配的時候,觸發了MinorGC,回收掉了之前的8M對象,此時年輕代為4M,老年代為2M,Survivor里面不知道是什么東西。。。。。。
  41.  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM