Java虚拟机与垃圾回收知识点梳理(一)Java内存区域与HotSpot虚拟机中堆中对象的创建,存储,访问
Java虚拟机与垃圾回收知识点梳理(二)垃圾收集算法
Java虚拟机与垃圾回收知识点梳理(三)HotSpot虚拟机的七种垃圾收集器及它们之间的关系
Java虚拟机与垃圾回收知识点梳理(四)内存分配与回收策略和垃圾收集器实例演示
Java虚拟机与垃圾回收知识点梳理(五)虚拟机类加载机制深入解析
内存分配与回收策略和垃圾收集器实例演示
内存分配与回收策略
从概念上来讲,在堆上分配。在经典分代的设计下,新生对象一般分配在新生代中;少数情况下(例如对象大小超过一定阈值)也可能会直接分配在老年代。《Java虚拟机规范》并未规定新对象的创建和存储细节,**这取决于虚拟机当前使用的是哪一种垃圾收集器,以及虚拟机中与内存相关的参数的设定。**本文以HotSpot虚拟机为例进行讲解。
对象优先在Eden分配
大多数情况下,对象在新生代Eden区中分配。当Eden区没有足够空间进行分配时,虚拟机将发起一次Minor GC。
大对象直接进入老年代
- 大对象就是指需要大量连续内存空间的Java对象,最典型的大对象便是那种很长的字符串,或者元素数量很庞大的数组,大对象对虚拟机的内存分配来说就是一个不折不扣的坏消息,比遇到一个大对象更加坏的消息就是遇到一群“朝生夕灭”的“短命大对象”。
- 在分配空间时,它容易导致内存明明还有不少空间时就提前触发垃圾收集,以获取足够的连续空间才能安置好它们。
- 当复制对象时,大对象就意味着高额的内存复制开销。HotSpot虚拟机提供了-XX:PretenureSizeThreshold参数,指定大于该设置值的对象直接在老年代分配,这样做的目的就是避免在Eden区及两个Survivor区之间来回复制,产生大量的内存复制操作。
注意 -XX:PretenureSizeThreshold参数只对Serial和ParNew两款新生代收集器有效,HotSpot的其他新生代收集器,如Parallel Scavenge并不支持这个参数。如果必须使用此参数进行调优,可考虑ParNew加CMS的收集器组合。
长期存活的对象将进入老年代
- HotSpot虚拟机中多数收集器都采用了分代收集来管理堆内存,那内存回收时就必须能决策哪些存活对象应当放在新生代,哪些存活对象放在老年代中。为做到这点,虚拟机给每个对象定义了一个对象年龄(Age)计数器,存储在对象头中。
- 对象通常在Eden区里诞生,如果经过第一次Minor GC后仍然存活,并且能被Survivor容纳的话,该对象会被移动到Survivor空间中,并且将其对象年龄设为1岁。对象在Survivor区中每熬过一次Minor GC,年龄就增加1岁,当它的年龄增加到一定程度(默认为15),就会被晋升到老年代中。对象晋升老年代的年龄阈值,可以通过参数-XX:MaxTenuringThreshold设置。
动态对象年龄判定
为了能更好地适应不同程序的内存状况,HotSpot虚拟机并不是永远要求对象的年龄必须达到-XX:MaxTenuringThreshold才能晋升老年代,如果在Survivor空间中相同年龄所有对象大小的总和大于Survivor空间的一半,年龄大于或等于该年龄的对象就可以直接进入老年代,无须等到-XX:MaxTenuringThreshold中要求的年龄。
空间分配担保
实战
如何查看当前Java版本默认的垃圾收集器
java -XX:+PrintCommandLineFlags -version
测试用例:
Serial收集器(串行收集器)案例
package test35;
import java.util.Random;
/**
* 分别设置最小堆内存,最大堆内存,开启GC日志打印,设置默认垃圾收集器为Serial垃圾收集器
* -Xms10m -Xmx10m -XX:+PrintGCDetails -XX:+PrintCommandLineFlags -XX:+UseSerialGC
*/
public class GCDemo {
public static void main(String[] args) {
System.out.println("****GC hello****");
try{
String str = "yemuxiaweiliang";
while (true){
str += str + new Random().nextInt(77777777)+new Random().nextInt(88888888);
str.intern();
}
}catch (Throwable e){
e.printStackTrace();
}
}
}
配置如下:
运行结果如下:
-XX:InitialHeapSize=10485760 -XX:MaxHeapSize=10485760 -XX:+PrintCommandLineFlags -XX:+PrintGCDetails -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:-UseLargePagesIndividualAllocation -XX:+UseSerialGC
****GC hello****
[GC (Allocation Failure) [DefNew: 2699K->320K(3072K), 0.0042329 secs] 2699K->1037K(9920K), 0.0055534 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [DefNew: 2628K->310K(3072K), 0.0021476 secs] 3346K->1687K(9920K), 0.0021799 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [DefNew: 2866K->3K(3072K), 0.0008978 secs] 4243K->2431K(9920K), 0.0009240 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [DefNew: 1003K->4K(3072K), 0.0005002 secs] 3432K->3420K(9920K), 0.0005158 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [DefNew: 1992K->4K(3072K), 0.0009111 secs] 5408K->5395K(9920K), 0.0009286 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [DefNew: 2111K->2111K(3072K), 0.0000175 secs][Tenured: 5391K->2956K(6848K), 0.0028599 secs] 7502K->2956K(9920K), [Metaspace: 3266K->3266K(1056768K)], 0.0029230 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [DefNew: 2004K->0K(3072K), 0.0009708 secs][Tenured: 4931K->3944K(6848K), 0.0031150 secs] 4960K->3944K(9920K), [Metaspace: 3304K->3304K(1056768K)], 0.0041272 secs] [Times: user=0.02 sys=0.00, real=0.00 secs]
[Full GC (Allocation Failure) [Tenured: 3944K->3786K(6848K), 0.0026273 secs] 3944K->3786K(9920K), [Metaspace: 3304K->3304K(1056768K)], 0.0026493 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap
def new generation total 3072K, used 109K [0x00000000ff600000, 0x00000000ff950000, 0x00000000ff950000)
eden space 2752K, 3% used [0x00000000ff600000, 0x00000000ff61b7b0, 0x00000000ff8b0000)
from space 320K, 0% used [0x00000000ff8b0000, 0x00000000ff8b0000, 0x00000000ff900000)
to space 320K, 0% used [0x00000000ff900000, 0x00000000ff900000, 0x00000000ff950000)
tenured generation total 6848K, used 3786K [0x00000000ff950000, 0x0000000100000000, 0x0000000100000000)
the space 6848K, 55% used [0x00000000ff950000, 0x00000000ffd028e0, 0x00000000ffd02a00, 0x0000000100000000)
Metaspace used 3371K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 363K, capacity 388K, committed 512K, reserved 1048576K
java.lang.OutOfMemoryError: Java heap space
Serial收集器对应的JVM参数是:-XX:+UseSerialGC
开启后会使用(新生代)Serial收集器+(老年代)Serial Old收集器
ParNew收集器案例
还是上面的那个案例,JVM参数修改如下所示:
-Xms10m -Xmx10m -XX:+PrintGCDetails -XX:+PrintCommandLineFlags -XX:+UseParNewGC
运行结果如下
-XX:InitialHeapSize=10485760 -XX:MaxHeapSize=10485760 -XX:+PrintCommandLineFlags -XX:+PrintGCDetails -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:-UseLargePagesIndividualAllocation -XX:+UseParNewGC
****GC hello****
[GC (Allocation Failure) [ParNew: 2742K->320K(3072K), 0.0035263 secs] 2742K->1058K(9920K), 0.0035570 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [ParNew: 2636K->320K(3072K), 0.0011897 secs] 3374K->1821K(9920K), 0.0012220 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [ParNew: 2917K->42K(3072K), 0.0008431 secs] 4418K->2550K(9920K), 0.0008723 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [ParNew: 1038K->10K(3072K), 0.0009951 secs] 3547K->4487K(9920K), 0.0010156 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [ParNew: 2011K->12K(3072K), 0.0010324 secs] 6488K->6456K(9920K), 0.0010559 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [ParNew: 2040K->2040K(3072K), 0.0000141 secs][Tenured: 6444K->3233K(6848K), 0.0028554 secs] 8485K->3233K(9920K), [Metaspace: 3360K->3360K(1056768K)], 0.0029074 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [ParNew (promotion failed): 1967K->1967K(3072K), 0.0004611 secs][Tenured: 5201K->4142K(6848K), 0.0028067 secs] 5201K->4142K(9920K), [Metaspace: 3360K->3360K(1056768K)], 0.0033008 secs] [Times: user=0.02 sys=0.00, real=0.00 secs]
[Full GC (Allocation Failure) [Tenured: 4142K->3802K(6848K), 0.0032716 secs] 4142K->3802K(9920K), [Metaspace: 3360K->3360K(1056768K)], 0.0032986 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap
par new generation total 3072K, used 83K [0x00000000ff600000, 0x00000000ff950000, 0x00000000ff950000)
eden space 2752K, 3% used [0x00000000ff600000, 0x00000000ff614d98, 0x00000000ff8b0000)
from space 320K, 0% used [0x00000000ff8b0000, 0x00000000ff8b0000, 0x00000000ff900000)
to space 320K, 0% used [0x00000000ff900000, 0x00000000ff900000, 0x00000000ff950000)
tenured generation total 6848K, used 3802K [0x00000000ff950000, 0x0000000100000000, 0x0000000100000000)
the space 6848K, 55% used [0x00000000ff950000, 0x00000000ffd06af8, 0x00000000ffd06c00, 0x0000000100000000)
Metaspace used 3391K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 365K, capacity 388K, committed 512K, reserved 1048576K
java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:3326)
at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:137)
at java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:121)
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:647)
at java.lang.StringBuilder.append(StringBuilder.java:208)
at test35.GCDemo.main(GCDemo.java:16)
Java HotSpot(TM) 64-Bit Server VM warning: Using the ParNew young collector with the Serial old collector is deprecated and will likely be removed in a future release
ParNew收集器对应的JVM参数是:-XX:+UseParNewGC
开启后会使用(新生代)ParNew收集器+(老年代)Serial Old收集器
Java HotSpot™ 64-Bit Server VM warning: Using the ParNew young collector with the Serial old collector is deprecated and will likely be removed in a future release(不推荐使用)
-XX:ParallelGCThreads限制线程数量,默认开启和CPU数量相同的线程数
Parallel Scavenge收集器(新生代)和Parallel Old收集器(老年代)案例
还是上面的那个案例,JVM参数修改如下所示:
-Xms10m -Xmx10m -XX:+PrintGCDetails -XX:+PrintCommandLineFlags -XX:+UseParallelGC
-Xms10m -Xmx10m -XX:+PrintGCDetails -XX:+PrintCommandLineFlags -XX:+UseParallelOldGC
运行结果如下:
-XX:InitialHeapSize=10485760 -XX:MaxHeapSize=10485760 -XX:+PrintCommandLineFlags -XX:+PrintGCDetails -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:-UseLargePagesIndividualAllocation -XX:+UseParallelGC
[GC (Allocation Failure) [PSYoungGen: 2048K->504K(2560K)] 2048K->962K(9728K), 0.0141004 secs] [Times: user=0.09 sys=0.00, real=0.01 secs]
****GC hello****
[GC (Allocation Failure) [PSYoungGen: 2410K->504K(2560K)] 2868K->1450K(9728K), 0.0011513 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 2025K->504K(2560K)] 2972K->2704K(9728K), 0.0011886 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 2072K->504K(2560K)] 4272K->3204K(9728K), 0.0008803 secs] [Times: user=0.03 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 2008K->488K(2560K)] 6675K->5203K(9728K), 0.0007743 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (Ergonomics) [PSYoungGen: 2509K->0K(2560K)] [ParOldGen: 6682K->4034K(7168K)] 9192K->4034K(9728K), [Metaspace: 3360K->3360K(1056768K)], 0.0045145 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[GC (Allocation Failure) [PSYoungGen: 0K->0K(1536K)] 4034K->4034K(8704K), 0.0002828 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (Allocation Failure) [PSYoungGen: 0K->0K(1536K)] [ParOldGen: 4034K->3902K(7168K)] 4034K->3902K(8704K), [Metaspace: 3360K->3360K(1056768K)], 0.0081027 secs] [Times: user=0.11 sys=0.00, real=0.01 secs]
Heap
PSYoungGen total 1536K, used 35K [0x00000000ffd00000, 0x0000000100000000, 0x0000000100000000)
eden space 1024K, 3% used [0x00000000ffd00000,0x00000000ffd08f20,0x00000000ffe00000)
from space 512K, 0% used [0x00000000fff80000,0x00000000fff80000,0x0000000100000000)
to space 1024K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000fff00000)
ParOldGen total 7168K, used 3902K [0x00000000ff600000, 0x00000000ffd00000, 0x00000000ffd00000)
object space 7168K, 54% used [0x00000000ff600000,0x00000000ff9cfa30,0x00000000ffd00000)
Metaspace used 3391K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 365K, capacity 388K, committed 512K, reserved 1048576K
java.lang.OutOfMemoryError: Java heap space
-XX:InitialHeapSize=10485760 -XX:MaxHeapSize=10485760 -XX:+PrintCommandLineFlags -XX:+PrintGCDetails -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:-UseLargePagesIndividualAllocation -XX:+UseParallelGC -XX:+UseParallelOldGC
[GC (Allocation Failure) [PSYoungGen: 2048K->504K(2560K)] 2048K->979K(9728K), 0.0100854 secs] [Times: user=0.06 sys=0.00, real=0.01 secs]
****GC hello****
[GC (Allocation Failure) [PSYoungGen: 2432K->504K(2560K)] 2908K->1406K(9728K), 0.0030800 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 2054K->504K(2560K)] 2956K->2709K(9728K), 0.0020146 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 2067K->496K(2560K)] 4273K->3220K(9728K), 0.0116510 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[GC (Allocation Failure) [PSYoungGen: 2014K->472K(2560K)] 6720K->5186K(9728K), 0.0017428 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (Ergonomics) [PSYoungGen: 2504K->0K(2560K)] [ParOldGen: 6696K->3855K(7168K)] 9201K->3855K(9728K), [Metaspace: 3360K->3360K(1056768K)], 0.0069533 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[GC (Allocation Failure) [PSYoungGen: 0K->0K(1536K)] 3855K->3855K(8704K), 0.0004006 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (Allocation Failure) [PSYoungGen: 0K->0K(1536K)] [ParOldGen: 3855K->3824K(7168K)] 3855K->3824K(8704K), [Metaspace: 3360K->3360K(1056768K)], 0.0120965 secs] [Times: user=0.08 sys=0.00, real=0.01 secs]
Heap
PSYoungGen total 1536K, used 46K [0x00000000ffd00000, 0x0000000100000000, 0x0000000100000000)
eden space 1024K, 4% used [0x00000000ffd00000,0x00000000ffd0b8f8,0x00000000ffe00000)
from space 512K, 0% used [0x00000000fff80000,0x00000000fff80000,0x0000000100000000)
to space 1024K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000fff00000)
ParOldGen total 7168K, used 3824K [0x00000000ff600000, 0x00000000ffd00000, 0x00000000ffd00000)
object space 7168K, 53% used [0x00000000ff600000,0x00000000ff9bc080,0x00000000ffd00000)
Metaspace used 3391K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 365K, capacity 388K, committed 512K, reserved 1048576K
java.lang.OutOfMemoryError: Java heap space
Parallel Scavenge收集器对应的JVM参数是:-XX:+UseParallelGC
Parallel Old收集器对应的JVM参数是:-XX:+UseParallelOldGC
开启上面任意一个后都会使用(新生代)Parallel Scavenge收集器+(老年代)Parallel Old收集器
CMS收集器(老年代)案例
还是上面的那个案例,JVM参数修改如下所示:
-Xms10m -Xmx10m -XX:+PrintGCDetails -XX:+PrintCommandLineFlags -XX:+UseConcMarkSweepGC
运行结果如下:
-XX:InitialHeapSize=10485760 -XX:MaxHeapSize=10485760 -XX:MaxNewSize=3497984 -XX:MaxTenuringThreshold=6 -XX:NewSize=3497984 -XX:OldPLABSize=16 -XX:OldSize=6987776 -XX:+PrintCommandLineFlags -XX:+PrintGCDetails -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseConcMarkSweepGC -XX:-UseLargePagesIndividualAllocation -XX:+UseParNewGC
****GC hello****
[GC (Allocation Failure) [ParNew: 2697K->320K(3072K), 0.0305853 secs] 2697K->1061K(9920K), 0.0306564 secs] [Times: user=0.19 sys=0.00, real=0.03 secs]
[GC (Allocation Failure) [ParNew: 2658K->320K(3072K), 0.0040375 secs] 3400K->1782K(9920K), 0.0041029 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [ParNew: 2876K->39K(3072K), 0.0005348 secs] 4339K->2493K(9920K), 0.0005671 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [ParNew: 1042K->12K(3072K), 0.0005740 secs] 3496K->3456K(9920K), 0.0005956 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (CMS Initial Mark) [1 CMS-initial-mark: 3444K(6848K)] 5449K(9920K), 0.0001714 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[CMS-concurrent-mark-start]
[GC (Allocation Failure) [ParNew: 2005K->14K(3072K), 0.0011635 secs] 5449K->5437K(9920K), 0.0011894 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[CMS-concurrent-mark: 0.001/0.002 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[CMS-concurrent-preclean-start]
[CMS-concurrent-preclean: 0.000/0.000 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (CMS Final Remark) [YG occupancy: 1091 K (3072 K)][Rescan (parallel) , 0.0012262 secs][weak refs processing, 0.0000380 secs][class unloading, 0.0002900 secs][scrub symbol table, 0.0004109 secs][scrub string table, 0.0001251 secs][1 CMS-remark: 5423K(6848K)] 6515K(9920K), 0.0021913 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[CMS-concurrent-sweep-start]
[CMS-concurrent-sweep: 0.001/0.001 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[CMS-concurrent-reset-start]
[CMS-concurrent-reset: 0.000/0.000 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [ParNew (promotion failed): 2158K->2161K(3072K), 0.0003869 secs][CMS: 3442K->2842K(6848K), 0.0032176 secs] 5601K->2842K(9920K), [Metaspace: 3347K->3347K(1056768K)], 0.0036536 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [ParNew: 2013K->4K(3072K), 0.0004949 secs][CMS: 4824K->3832K(6848K), 0.0041238 secs] 4856K->3832K(9920K), [Metaspace: 3355K->3355K(1056768K)], 0.0046761 secs] [Times: user=0.01 sys=0.00, real=0.00 secs]
[Full GC (Allocation Failure) [CMS: 3832K->3800K(6848K), 0.0039923 secs] 3832K->3800K(9920K), [Metaspace: 3355K->3355K(1056768K)], 0.0040291 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (CMS Initial Mark) [1 CMS-initial-mark: 3800K(6848K)] 3804K(9920K), 0.0001281 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[CMS-concurrent-mark-start]
[CMS-concurrent-mark: 0.001/0.001 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[CMS-concurrent-preclean-start]
[CMS-concurrent-preclean: 0.000/0.000 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap
par new generation total 3072K, used 85K [0x00000000ff600000, 0x00000000ff950000, 0x00000000ff950000)
eden space 2752K, 3% used [0x00000000ff600000, 0x00000000ff615738, 0x00000000ff8b0000)
from space 320K, 0% used [0x00000000ff900000, 0x00000000ff900000, 0x00000000ff950000)
to space 320K, 0% used [0x00000000ff8b0000, 0x00000000ff8b0000, 0x00000000ff900000)
concurrent mark-sweep generation total 6848K, used 3800K [0x00000000ff950000, 0x0000000100000000, 0x0000000100000000)
Metaspace used 3391K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 365K, capacity 388K, committed 512K, reserved 1048576K
[GC (CMS Final Remark) [YG occupancy: 85 K (3072 K)][Rescan (parallel) , 0.0003527 secs][weak refs processing, 0.0000061 secs][class unloading, 0.0002414 secs][scrub symbol table, 0.0003151 secs][scrub string table, 0.0001133 secs][1 CMS-remark: 3800K(6848K)] 3886K(9920K), 0.0011023 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[CMS-concurrent-sweep-start]
java.lang.OutOfMemoryError: Java heap space
CMS收集器对应的JVM参数是:-XX:+UseConcMarkSweepGC
开启此参数后会使用(新生代)ParNew收集器+(老年代)CMS收集器+Serail Old收集器的组合。
G1收集器案例
还是上面的那个案例,JVM参数修改如下所示:
-Xms10m -Xmx10m -XX:+PrintGCDetails -XX:+PrintCommandLineFlags -XX:+UseG1GC
运行结果如下:
-XX:InitialHeapSize=10485760 -XX:MaxHeapSize=10485760 -XX:+PrintCommandLineFlags -XX:+PrintGCDetails -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseG1GC -XX:-UseLargePagesIndividualAllocation
****GC hello****
[GC pause (G1 Evacuation Pause) (young), 0.0018454 secs]
[Parallel Time: 1.4 ms, GC Workers: 8]
[GC Worker Start (ms): Min: 193.5, Avg: 193.6, Max: 193.9, Diff: 0.5]
[Ext Root Scanning (ms): Min: 0.0, Avg: 0.2, Max: 0.3, Diff: 0.3, Sum: 1.3]
[Update RS (ms): Min: 0.0, Avg: 0.1, Max: 0.2, Diff: 0.2, Sum: 0.6]
[Processed Buffers: Min: 0, Avg: 1.0, Max: 2, Diff: 2, Sum: 8]
[Scan RS (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.0]
[Code Root Scanning (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.0]
[Object Copy (ms): Min: 0.8, Avg: 1.0, Max: 1.0, Diff: 0.2, Sum: 7.8]
[Termination (ms): Min: 0.0, Avg: 0.0, Max: 0.1, Diff: 0.1, Sum: 0.3]
[GC Worker Other (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.2]
[GC Worker Total (ms): Min: 0.9, Avg: 1.3, Max: 1.4, Diff: 0.5, Sum: 10.3]
[GC Worker End (ms): Min: 194.8, Avg: 194.8, Max: 194.9, Diff: 0.0]
[Code Root Fixup: 0.0 ms]
[Code Root Migration: 0.0 ms]
[Clear CT: 0.1 ms]
[Other: 0.3 ms]
[Choose CSet: 0.0 ms]
[Ref Proc: 0.2 ms]
[Ref Enq: 0.0 ms]
[Free CSet: 0.0 ms]
[Eden: 6144.0K(6144.0K)->0.0B(3072.0K) Survivors: 0.0B->1024.0K Heap: 7131.8K(10.0M)->3331.8K(10.0M)]
[Times: user=0.00 sys=0.00, real=0.00 secs]
/
[GC pause (G1 Humongous Allocation) (young) (initial-mark), 0.0012106 secs]
[Parallel Time: 1.0 ms, GC Workers: 8]
[GC Worker Start (ms): Min: 195.9, Avg: 196.3, Max: 196.9, Diff: 0.9]
[Ext Root Scanning (ms): Min: 0.0, Avg: 0.1, Max: 0.3, Diff: 0.3, Sum: 0.9]
[Code Root Marking (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.0]
[Update RS (ms): Min: 0.0, Avg: 0.2, Max: 0.3, Diff: 0.3, Sum: 1.4]
[Processed Buffers: Min: 0, Avg: 2.1, Max: 5, Diff: 5, Sum: 17]
[Scan RS (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.0]
[Code Root Scanning (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.0]
[Object Copy (ms): Min: 0.0, Avg: 0.3, Max: 0.5, Diff: 0.5, Sum: 2.1]
[Termination (ms): Min: 0.0, Avg: 0.0, Max: 0.1, Diff: 0.1, Sum: 0.3]
[GC Worker Other (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.1]
[GC Worker Total (ms): Min: 0.0, Avg: 0.6, Max: 1.0, Diff: 1.0, Sum: 4.7]
[GC Worker End (ms): Min: 196.9, Avg: 196.9, Max: 196.9, Diff: 0.0]
[Code Root Fixup: 0.0 ms]
[Code Root Migration: 0.0 ms]
[Clear CT: 0.1 ms]
[Other: 0.2 ms]
[Choose CSet: 0.0 ms]
[Ref Proc: 0.1 ms]
[Ref Enq: 0.0 ms]
[Free CSet: 0.0 ms]
[Eden: 1024.0K(3072.0K)->0.0B(2048.0K) Survivors: 1024.0K->1024.0K Heap: 4854.5K(10.0M)->4512.9K(10.0M)]
[Times: user=0.00 sys=0.00, real=0.00 secs]
[GC concurrent-root-region-scan-start]
[GC concurrent-root-region-scan-end, 0.0000331 secs]
[GC concurrent-mark-start]
[GC concurrent-mark-end, 0.0013285 secs]
[GC remark [GC ref-proc, 0.0000722 secs], 0.0070590 secs]
[Times: user=0.00 sys=0.00, real=0.01 secs]
[GC cleanup 7496K->6509K(10M), 0.0002151 secs]
[Times: user=0.00 sys=0.00, real=0.00 secs]
[GC concurrent-cleanup-start]
[GC concurrent-cleanup-end, 0.0000076 secs]
///中间省略
Heap
garbage-first heap total 10240K, used 6778K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
region size 1024K, 1 young (1024K), 0 survivors (0K)
Metaspace used 3396K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 365K, capacity 388K, committed 512K, reserved 1048576K
java.lang.OutOfMemoryError: Java heap space
CMS收集器对应的JVM参数是:-XX:+UseG1GC