Serial Old收集器是JAVA虛擬機中垃圾收集器的一種,它是Serial收集器的老年代版本,它同樣是一個單線程收集器,使用“標記-整理”算法。這個收集器的主要也是在目前的JAVA的Client模式下的虛擬機使用。如果在Server模式下,它主要還有兩大用途:一個是在JDK 1.5及之前的版本中與Parallel Scavenge收集器搭配使用,另外一個就是作為CMS收集器的後備預案。如果CMS收集器出現Concurrent Mode Failure,則Serial Old收集器將作為後備收集器。
1、簡單示例
源代碼
package com.gc;
import java.util.ArrayList;
import java.util.List;
/**
* 簡單的JAVA虛擬機內存回收,serial old收集器的使用
* 參數:-Xms30m -Xmx100m-Xmn10m -XX:+UseSerialGC -XX:+PrintGCDetails
* @author 范芳銘
*/
public class EasySerialOld {
public byte[] placeHolder =new byte[64 * 1024]; //占位符
public static voidmain(String[] args) throws Exception{
outOfMemoryByExpansionSize();
}
private static voidoutOfMemoryByExpansionSize() throws Exception{
List<EasySerialOld>list = new ArrayList<EasySerialOld>();
while(true){
EasySerialOldserial = new EasySerialOld();
list.add(serial);
Thread.sleep(10);//停頓10毫秒
}
}
}
參數
-Xms30m JVM最小30M
-Xmx100m JVM最大100M
-Xmn10m 新生代固定10M
-XX:+UseSerialGC 對應serial + serial old 收集器
-XX:+PrintGCDetails 打印詳細GC
運行結果
[GC [DefNew: 8137K->1023K(9216K),0.0070798 secs] 8137K->8019K(29696K), 0.0071114 secs] [Times: user=0.00sys=0.00, real=0.01 secs]
[GC [DefNew: 9175K->963K(9216K),0.0056184 secs] 16170K->16151K(29696K), 0.0056422 secs] [Times: user=0.00sys=0.00, real=0.00 secs]
[GC [DefNew: 9149K->964K(9216K),0.0050408 secs][Tenured: 23320K->23388K(23424K), 0.0030744 secs]24337K->24284K(32640K), [Perm : 2086K->2086K(12288K)], 0.0082092 secs][Times: user=0.00 sys=0.00, real=0.01 secs]
[GC [DefNew: 8166K->964K(9216K),0.0052883 secs] 31555K->31522K(48200K), 0.0053220 secs] [Times: user=0.00sys=0.00, real=0.01 secs]
[GC [DefNew: 9118K->962K(9216K),0.0050018 secs] 39676K->39654K(48200K), 0.0050367 secs] [Times: user=0.00sys=0.02, real=0.00 secs]
[GC [DefNew: 9108K->966K(9216K),0.0049332 secs][Tenured: 46824K->46893K(46920K), 0.0033687 secs]47800K->47790K(56136K), [Perm : 2086K->2086K(12288K)], 0.0084489 secs][Times: user=0.02 sys=0.00, real=0.01 secs]
[GC [DefNew: 8141K->963K(9216K),0.0056947 secs] 55034K->55026K(87372K), 0.0057206 secs] [Times: user=0.00sys=0.00, real=0.01 secs]
[GC [DefNew: 9099K->962K(9216K),0.0046619 secs] 63162K->63158K(87372K), 0.0046951 secs] [Times: user=0.00sys=0.00, real=0.00 secs]
[GC [DefNew: 9102K->968K(9216K),0.0048499 secs] 71298K->71296K(87372K), 0.0048757 secs] [Times: user=0.00sys=0.00, real=0.00 secs]
[GC [DefNew: 9104K->962K(9216K),0.0053368 secs][Tenured: 78465K->78531K(78540K), 0.0035892 secs]79431K->79427K(87756K), [Perm : 2086K->2086K(12288K)], 0.0090187 secs][Times: user=0.00 sys=0.00, real=0.01 secs]
[GC [DefNew: 8134K->962K(9216K),0.0061709 secs] 86665K->86663K(101376K), 0.0061952 secs] [Times: user=0.00sys=0.00, real=0.01 secs]
[GC [DefNew: 9096K->9096K(9216K),0.0000242 secs][Tenured: 85701K->92157K(92160K), 0.0230609 secs]94797K->94783K(101376K), [Perm : 2086K->2084K(12288K)], 0.0231402 secs][Times: user=0.03 sys=0.00, real=0.02 secs]
[Full GC [Tenured:92157K->92157K(92160K), 0.0044209 secs] 101264K->101260K(101376K), [Perm: 2084K->2084K(12288K)], 0.0044763 secs] [Times: user=0.00 sys=0.00,real=0.00 secs]
[Full GC [Tenured:92157K->92151K(92160K), 0.0072855 secs] 101260K->101254K(101376K), [Perm: 2084K->2084K(12288K)], 0.0073138 secs] [Times: user=0.00 sys=0.00,real=0.01 secs]
Exception in thread "main"java.lang.OutOfMemoryError: Java heap space
atcom.gc.EasyParNew.<init>(EasyParNew.java:12)
atcom.gc.EasyParNew.outOfMemoryByExpansionSize(EasyParNew.java:39)
atcom.gc.EasyParNew.main(EasyParNew.java:14)
Heap
def new generation total 9216K, used 9152K [0x03b70000,0x04570000, 0x04570000)
eden space 8192K, 100% used [0x03b70000, 0x04370000, 0x04370000)
from space 1024K, 93% used[0x04470000, 0x045600f0, 0x04570000)
to space 1024K, 0% used [0x04370000, 0x04370000, 0x04470000)
tenured generation total 92160K, used 92151K [0x04570000,0x09f70000, 0x09f70000)
the space 92160K, 99% used[0x04570000, 0x09f6de68, 0x09f6e000, 0x09f70000)
compacting perm gen total 12288K, used 2105K [0x09f70000,0x0ab70000, 0x0df70000)
結果分析
[GC [DefNew: 9149K->964K(9216K),0.0050408 secs][Tenured: 23320K->23388K(23424K), 0.0030744 secs]24337K->24284K(32640K), [Perm : 2086K->2086K(12288K)], 0.0082092 secs][Times: user=0.00 sys=0.00, real=0.01 secs]
Tenured:23320K->23388K(23424K) ,老年代在回收垃圾(本代碼用來做內存溢出是好手…),空間沒有了,但是JVM最大劃分了100M,可以繼續調配空間。於是又來了第二次:
[GC [DefNew: 9104K->962K(9216K),0.0053368 secs][Tenured: 78465K->78531K(78540K), 0.0035892 secs]79431K->79427K(87756K), [Perm : 2086K->2086K(12288K)], 0.0090187 secs][Times: user=0.00 sys=0.00, real=0.01 secs]
Tenured: 78465K->78531K(78540K) ,老年代空間明顯變大了… 很快,100M空間就用完了
用[Full GC[Tenured: 92157K->92157K(92160K), 0.0044209 secs] 101264K->101260K(101376K),[Perm : 2084K->2084K(12288K)], 0.0044763 secs] [Times: user=0.00 sys=0.00,real=0.00 secs]進行強力回收,也沒有用,於是掛掉了…