Java 虚拟机系列(三)垃圾收集器总结

本文总结下垃圾收集涉及的一些重点:

gc_summary

基于分代收集算法的垃圾收集器组合,总结如下图,常用于 JDK 8 及之前的版本:

generational_collection

GC 选项配置

https://docs.oracle.com/javase/8/docs/technotes/tools/unix/java.html

  • DefNew – single-threaded mark-copy stop-the-world garbage collector and is what is used to clean the Young generation(单线程 (single-threaded), 采用标记-复制 (mark-copy) 算法的,使整个 JVM 暂停运行 (stop-the-world) 的新生代 (Young generation) 垃圾收集器 (garbage collector))
Advanced Garbage Collection Options GC Configuration Description
-XX:+UseSerialGC Serial + Serial Old Enables the use of the serial garbage collector. This is generally the best choice for small and simple applications that do not require any special functionality from garbage collection.
By default, this option is disabled and the collector is chosen automatically based on the configuration of the machine and type of the JVM.
-XX:+UseParNewGC ParNew + SerialOld Enables the use of parallel threads for collection in the young generation.
By default, this option is disabled. It is automatically enabled when you set the -XX:+UseConcMarkSweepGC option. Using the -XX:+UseParNewGC option without the -XX:+UseConcMarkSweepGC option was deprecated in JDK 8.
Java 8 JEP 173: Retire Some Rarely-Used GC Combinations
Java 9 JEP 214: Remove GC Combinations Deprecated in JDK 8
-XX:+UseConcMarkSweepGC ParNew + CMS Enables the use of the CMS garbage collector for the old generation. Oracle recommends that you use the CMS garbage collector when application latency requirements cannot be met by the throughput (-XX:+UseParallelGC) garbage collector. The G1 garbage collector (-XX:+UseG1GC) is another alternative.
By default, this option is disabled and the collector is chosen automatically based on the configuration of the machine and type of the JVM.
When this option is enabled, the -XX:+UseParNewGC option is automatically set and you should not disable it, because the following combination of options has been deprecated in JDK 8: -XX:+UseConcMarkSweepGC -XX:-UseParNewGC. (Serial + CMS)
Java 9 JEP 291: Deprecate the Concurrent Mark Sweep (CMS) Garbage Collector
Java 14 JEP 363: Remove the Concurrent Mark Sweep (CMS) Garbage Collector
-XX:+UseParallelGC Parallel Scavenge + Parallel Old Enables the use of the parallel scavenge garbage collector (also known as the throughput collector) to improve the performance of your application by leveraging multiple processors.
By default, this option is disabled and the collector is chosen automatically based on the configuration of the machine and type of the JVM. If it is enabled, then the -XX:+UseParallelOldGC option is automatically enabled, unless you explicitly disable it.
Java 14 JEP 366: Deprecate the ParallelScavenge + SerialOld GC Combination
-XX:+UseParallelOldGC Parallel Scavenge + Parallel Old Enables the use of the parallel garbage collector for full GCs.
By default, this option is disabled. Enabling it automatically enables the -XX:+UseParallelGC option.
-XX:+UseG1GC Enables the use of the garbage-first (G1) garbage collector. It is a server-style garbage collector, targeted for multiprocessor machines with a large amount of RAM. It meets GC pause time goals with high probability, while maintaining good throughput. The G1 collector is recommended for applications requiring large heaps (sizes of around 6 GB or larger) with limited GC latency requirements (stable and predictable pause time below 0.5 seconds).
By default, this option is disabled and the collector is chosen automatically based on the configuration of the machine and type of the JVM.
-XX:UseZGC

G1

Garbage First Garbage Collector Tuning - Oracle

GC 日志

Java 9 JEP 158: Unified JVM Logging

Java 9 JEP 271: Unified GC Logging

在线 GC 日志分析工具:https://gceasy.io/

1
-XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+PrintHeapAtGC -Xloggc:E:/logs/gc-default.log

参考

HotSpot Virtual Machine Garbage Collection Tuning Guide - Java 17

Available Collectors

  • Serial Collector
  • Parallel Collector
  • Garbage-First (G1) Garbage Collector
  • The Z Garbage Collector

https://www.baeldung.com/java-verbose-gc

《深入理解 Java 虚拟机》

CMS 和 G1 改用三色标记法,可达性分析到底做错了什么?