The GC algorithm is the methodology of memory recycling, and the garbage collector is the implementation of the algorithm.
Four main garbage collector:
⑴ serial garbage collector 只使用一个线程进行垃圾回收,会暂停所有的用户线程
(Serial): ;
⑵ parallel garbage collector 多个垃圾收集器线程并行工作,会暂停所有的用户线程
(Parallel): ;
⑶ concurrent garbage collector (the 用户线程和垃圾收集器线程同时执行,用户程序在继续运行,垃圾收集程序线程运行于另一个CPU上
CMS): ;
⑷ G1 garbage 将堆内存分割成不同的区域,并发对其进行垃圾回收
collector: ;
View the default garbage collector: java -XX:+PrintCommandLIneFlags -version
;
New generation collector: Serial, ParNew, Parallel Scavenge;
old generation collector: Serial Old, Parallel Old, CMS;
whole heap collector: G1;
Note:
no matter which one is used, the new generation uses the replication algorithm; the
old generation uses CMS Is a mark removal algorithm, others use a mark compression algorithm;
Serial (serial) : 最基本、发展历史最悠久的收集器
;
ParNew (parallel) : Serial收集器的多线程版本
;
Parallel Scavenge (parallel) : 与吞吐量关系密切,也称为吞吐量收集器
;
Serial Old (serial) : Serial收集器的老年代版本
;
Parallel Old (parallel): Parallel Scavenge收集器的老年代版本
;;
CMS (concurrent) : 也称为并发低停顿收集器(Concurrent Low Pause Collector)或低延迟(low-latency)垃圾收集器
;
CMS four-step process:
1. Initial marker: 仅标记GC Roots能直接关联到的对象,速度很快, 但需要"Stop The World"
;
2 concurrent mark: 从第一步标记的对象出发,并发标记可达对象 (用户线程不会暂停)
;
3 relabeled: 修正并发标记期间因用户程序继续运作而导致标记变动的那一部分对象的标记记录,需要"Stop The World"
;
4. Concurrent Clear: 回收所有的垃圾对象
;
Setting parameters:
Set parameters for the new generation | Cenozoic | Old age |
---|---|---|
-XX:+UseSerialGC | Serial GC | Serial Old GC |
-XX:+UseParNewGC | ParNew GC | Serial Old GC |
-XX:+UseParallelGC | ParNew Scavenge GC | Parallel Old GC |
-XX:+UseConcMarkSweepGC | ParNew GC | CMS |
How to choose the garbage collector:
single or small CPU Memory: -XX:+UseSerialGC
;
multi-CPU pursuit of high throughput (calculated as background application): -XX:+UseParallelGC
;
multi-CPU pursuit of low pause time: -XX:+UseConcMarkSweepGC
;
Reference connection:
https://www.cnblogs.com/cxxjohnson/p/8625713.html