先上代碼:
主函數:
-
public class GCDemo {
-
-
public static void main(String[] args) throws InterruptedException{
-
-
-
List<GCDataObject> list = new ArrayList<GCDataObject>();
-
for(int i = 0 ; i < 4 ; i++){
-
list.add( new GCDataObject(2));
-
}
-
-
//表示第二次fullGC,或者MinorGC可以把他回收!
-
list = null;
-
-
-
-
List<GCDataObject> list2 = new ArrayList<GCDataObject>();
-
for(int i = 0 ; i < 3 ; i++){
-
list2.add( new GCDataObject(2));
-
}
-
list2 = null;
-
-
-
}
-
}
對象(好像直接內部類不好~):
-
public class GCDataObject {
-
byte[] bytes = null;
-
public GCDataObject(int i){
-
bytes = new byte[i *1024 * 1024];
-
}
-
}
為調優的前的參數:
-
-Xms20M -Xmx20M -Xmn10M -verbose:gc -XX:+PrintGCDetails -XX:+UseSerialGC
-
-
打印的日志:
-
[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]
-
[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]
-
Heap
-
def new generation total 9216K, used 2130K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
-
eden space 8192K, 26% used [0x00000000fec00000, 0x00000000fee14930, 0x00000000ff400000)
-
from space 1024K, 0% used [0x00000000ff500000, 0x00000000ff500000, 0x00000000ff600000)
-
to space 1024K, 0% used [0x00000000ff400000, 0x00000000ff400000, 0x00000000ff500000)
-
tenured generation total 10240K, used 4618K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
-
the space 10240K, 45% used [0x00000000ff600000, 0x00000000ffa82990, 0x00000000ffa82a00, 0x0000000100000000)
-
Metaspace used 2582K, capacity 4486K, committed 4864K, reserved 1056768K
-
class space used 287K, capacity 386K, committed 512K, reserved 1048576K
-
-
大致的過程:
-
首先向虛擬機分配的4個2M的對象,當中因為參數的設置,新生代Eden為8M,兩個Survivor為1M,中間發生了1次MinorGC,是以Eden空間不足導致,而且都是立馬從Survivor再次發生MinorGC進入到了舊生代,第一次FullGC清理掉了之前的8M,此時內存分配情況為新生代的Eden為2M(有誤差),舊生代為4M,
-
-
解決方法:
-
-
因為FullGC非常的耗時,需要盡量減少它的次數。但是又不得不MinorGC,所以加大年輕代的空間,在撐到第一個集合的對象分配完后,在下一次分配集合后發生MinonrGC就把 之前的對象回收掉。
-
同樣利用上面的代碼(業務邏輯不變),修改的參數為:
-
-
-Xms20M -Xmx20M -Xmn16M -verbose:gc -XX:+PrintGCDetails -XX:+UseSerialGC
-
-
[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]
-
Heap
-
def new generation total 14784K, used 5006K [0x00000000fec00000, 0x00000000ffc00000, 0x00000000ffc00000)
-
eden space 13184K, 34% used [0x00000000fec00000, 0x00000000ff060bc0, 0x00000000ff8e0000)
-
from space 1600K, 32% used [0x00000000ffa70000, 0x00000000ffaf2f20, 0x00000000ffc00000)
-
to space 1600K, 0% used [0x00000000ff8e0000, 0x00000000ff8e0000, 0x00000000ffa70000)
-
tenured generation total 4096K, used 2048K [0x00000000ffc00000, 0x0000000100000000, 0x0000000100000000)
-
the space 4096K, 50% used [0x00000000ffc00000, 0x00000000ffe00010, 0x00000000ffe00200, 0x0000000100000000)
-
Metaspace used 2582K, capacity 4486K, committed 4864K, reserved 1056768K
-
class space used 287K, capacity 386K, committed 512K, reserved 1048576K
-
-
從之前的一次MinorGC和一次FullGC減少為一次MinorGC。
-
-
大致過程:
-
因為調整了年輕代大小,為16M,Eden為12.8M,Survivor為1.6M,第一次的集合分配,整整8M分配進了Eden,在下次集合的分配的時候,觸發了MinorGC,回收掉了之前的8M對象,此時年輕代為4M,老年代為2M,Survivor里面不知道是什么東西。。。。。。
-