Detailed Explanation of JVM Garbage Collectors in Java

Detailed Explanation of JVM Garbage Collectors in Java

1. Purpose and Classification of Garbage Collectors

The Garbage Collector (GC) is a core component of the JVM, responsible for automatically managing the allocation and reclamation of heap memory to prevent memory leaks. Based on their working modes, garbage collectors can be classified into the following categories:

  • Serial GC: Performs garbage collection using a single thread, suitable for scenarios with limited memory and few CPU cores.
  • Parallel GC: Uses multiple threads for parallel collection, aiming for high throughput (suitable for background computational tasks).
  • Concurrent GC: GC threads execute concurrently with user threads, reducing pause times (suitable for responsive applications).
  • G1 (Garbage-First) Collector: Designed for large heap memory, divides the heap into multiple Regions, and optimizes collection by predicting pause times.
  • ZGC/Shenandoah: Next-generation low-latency collectors with pause times not exceeding 10ms (JDK11+).

2. Working Principles of Common Garbage Collectors

2.1 Serial GC

  • Working Process:
    1. When GC is triggered, all user threads are suspended (Stop-The-World).
    2. Executes mark-sweep or mark-compact algorithms using a single thread.
  • Suitable Scenarios: Client mode or embedded devices.
  • JVM Parameters: -XX:+UseSerialGC

2.2 Parallel GC (Throughput-Oriented)

  • Working Process:
    1. Uses a parallel copying algorithm for the young generation, with multi-threaded collection.
    2. Uses a mark-compact algorithm for the old generation.
  • Characteristics: Improves collection speed through multi-threading but still involves Stop-The-World pauses.
  • JVM Parameters: -XX:+UseParallelGC

2.3 CMS (Concurrent Mark-Sweep)

  • Goal: Minimize pause times, divided into 4 phases:
    1. Initial Mark: Marks objects directly reachable from GC Roots (requires STW).
    2. Concurrent Mark: Traverses the object graph, executing concurrently with user threads.
    3. Remark: Corrects objects modified during the concurrent marking phase (requires STW).
    4. Concurrent Sweep: Cleans up garbage objects.
  • Drawbacks: Generates memory fragmentation and may trigger Full GC.
  • JVM Parameters: -XX:+UseConcMarkSweepGC

2.4 G1 (Garbage-First)

  • Core Idea: Divides the heap into multiple equally-sized Regions (default 2048), prioritizing the collection of regions with the most garbage.
  • Working Process:
    1. Young Generation Collection: Copies surviving objects to Survivor regions or old generation Regions.
    2. Mixed GC: Collects both young generation and some old generation Regions simultaneously.
    3. Full GC (Fallback): Degrades to Serial GC when collection cannot keep up with allocation speed.
  • Advantage: Predictable pause time model (configurable via -XX:MaxGCPauseMillis).
  • JVM Parameters: -XX:+UseG1GC

3. How to Choose a Garbage Collector?

  • Prioritize High Throughput: Parallel GC (e.g., for batch processing tasks).
  • Require Low Latency: CMS (for JDK8 and earlier) or G1 (default for JDK9+).
  • Very Large Heap Memory (exceeding 16GB): G1 or ZGC/Shenandoah.
  • Ultra-Low Pause (Millisecond Level): ZGC (mature in JDK15+) or Shenandoah.

4. Practical Tuning Parameter Examples

# Use G1 and set the maximum pause time  
java -XX:+UseG1GC -XX:MaxGCPauseMillis=200 -Xmx4g MyApp  

# Use Parallel GC and specify the number of threads  
java -XX:+UseParallelGC -XX:ParallelGCThreads=4 -Xmx2g MyApp  

5. Summary

Choosing a garbage collector requires balancing throughput, latency, and memory overhead. Modern applications (e.g., microservices) typically prioritize G1 or ZGC, while traditional batch processing tasks may be more suitable for Parallel GC. Understanding the working mechanisms of different collectors is fundamental to JVM tuning.