HotSpot共有7种垃圾收集器,3个新生代垃圾收集器,3个老年代垃圾收集器,以及G1,一共构成7种可供选择的垃圾收集器组合。
新生代与老年代垃圾收集器之间形成6种组合,每个新生代垃圾收集器都对应2种组合。
Serial (DefNew) + Serial Old(Serial Mark Sweep Compact)
Parallel (ParNew) + Serial Old(Serial Mark Sweep Compact)
Parallel Scavenge (PSYoungGen) + Serial Old(Serial Mark Sweep Compact (PSOldGen))
注:在Parallel Scavenge收集器架构中本身有PS MarkSweep收集器来进行老年代收集,但由于PS MarkSweep与Serial Old实现非常接近,因此官方的许多资料都直接以Serial Old代替PS MarkSweep进行讲解。
Parallel Scavenge (PSYoungGen) + Parallel Mark Sweep Compact (ParOldGen)
Serial (DefNew) + CMS(Concurrent Mark Sweep)
Parallel (ParNew) + CMS(Concurrent Mark Sweep) + Serial Old(Serial Mark Sweep Compact)
-XX:+UseG1GC
blogs.oracle.com/jonthecolle…
www.oracle.com/technetwork…
所有新生代垃圾收集器,都使用复制算法,都会发生stop-the-world。由于绝大多数对象的生命周期通常比较短,在新生代被回收的可能性很大,新生代的垃圾回收通常可以回收大部分对象,因此采用复制算法效率更高。
"Serial" is a stop-the-world, copying collector which uses a single GC thread.
ParNew 与 Parallel Scavenge的一个主要区别是,ParNew可以与CMS进行搭配使用。
"ParNew" is a stop-the-world, copying collector which uses multiple GC threads. It differs from "Parallel Scavenge" in that it has enhancements that make it usable with CMS. For example, "ParNew" does the synchronization needed so that it can run during the concurrent phases of CMS.
采用复制算法,GC时发生stop-the-world,使用多个GC线程。 吞吐量优先收集器 ,可控制最大垃圾收集停顿时间(-XX:MaxGCPauseMillis)与吞吐量大小(-XX:GCTimeRatio),支持GC自适应的调节策略(GC Ergonomics,对应参数-XX:+UseAdaptiveSizePolicy)。
"Parallel Scavenge" is a stop-the-world, copying collector which uses multiple GC threads.
"Serial Old" is a stop-the-world, mark-sweep-compact collector that uses a single GC thread.
"CMS" is a mostly concurrent, low-pause collector.
Java之CMS GC的7个阶段: mp.weixin.qq.com/s/vmnBlrM7p…
"Parallel Old" is a compacting collector that uses multiple GC threads.
G1将整个堆划分为多个大小相等的独立区域(Region),保留新生代和老年代的分代概念(但两者不再是物理隔离的)。
从整体来看是基于标记整理算法,从局部(两个Region之间)来看是基于复制算法。因此,可以避免产生内存空间碎片,防止发生并发模式失败。
The Garbage First or G1 garbage collector is available in Java 7 and is designed to be the long term replacement for the CMS collector. The G1 collector is a parallel, concurrent, and incrementally compacting low-pause garbage collector that has quite a different layout from the other garbage collectors described previously.
-XX:+UseConcMarkSweepGC(该参数隐式启用-XX:+UseParNewGC)