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]进行强力回收,也没有用,于是挂掉了…