Java.Core.What types of garbage collectors are implemented in the HotSpot virtual machine ?

Types of Garbage Collectors in the HotSpot JVM

The HotSpot JVM (Java Virtual Machine) implements multiple Garbage Collectors (GCs) to manage memory efficiently. Each GC has different strategies optimized for various workloads.


1. Overview of HotSpot Garbage Collectors

Garbage CollectorJVM OptionBest For
Serial GC-XX:+UseSerialGCSmall applications, single-threaded environments
Parallel GC (Throughput GC)-XX:+UseParallelGCMulti-threaded applications, high throughput
G1 (Garbage First) GC-XX:+UseG1GC (Default in Java 9+)General-purpose, large heaps
ZGC (Low-Latency GC)-XX:+UseZGCLow-latency, real-time applications
Shenandoah GC-XX:+UseShenandoahGCUltra-low pause time applications
Epsilon GC (No GC)-XX:+UseEpsilonGCPerformance testing, no memory cleanup

2. Types of Garbage Collectors

1️⃣ Serial Garbage Collector (Single-Threaded GC)

JVM Option: -XX:+UseSerialGC
Best for: Small applications, single-threaded environments
How it works:

  • Uses a stop-the-world approach (application stops during GC).
  • Works sequentially (uses only one thread).
  • Suitable for applications with small heaps (<1GB).

Example: Enable Serial GC

java -XX:+UseSerialGC MyApp

2️⃣ Parallel Garbage Collector (Throughput GC)

JVM Option: -XX:+UseParallelGC
Best for: High-throughput, multi-threaded applications
How it works:

  • Uses multiple threads to collect garbage in parallel.
  • Aims for high throughput by maximizing CPU usage.
  • Uses stop-the-world pauses, but faster than Serial GC.

Example: Enable Parallel GC

java -XX:+UseParallelGC MyApp

3️⃣ G1 (Garbage First) Garbage Collector (Default GC in Java 9+)

JVM Option: -XX:+UseG1GC
Best for: Large heaps (>4GB), balanced performance
How it works:

  • Divides the heap into regions instead of separate young/old generations.
  • Uses concurrent background threads for garbage collection.
  • Minimizes stop-the-world pauses by prioritizing most filled regions.

Example: Enable G1 GC

java -XX:+UseG1GC MyApp

4️⃣ Z Garbage Collector (ZGC)

JVM Option: -XX:+UseZGC
Best for: Low-latency applications (latency <10ms)
How it works:

  • Performs most of the GC work concurrently while the application is running.
  • Supports large heaps up to 16TB.
  • Pause times are independent of heap size (great for real-time applications).

Example: Enable ZGC

java -XX:+UseZGC MyApp

5️⃣ Shenandoah Garbage Collector

JVM Option: -XX:+UseShenandoahGC
Best for: Applications requiring ultra-low pause times
How it works:

  • Uses concurrent garbage collection to keep pauses low.
  • Pause times are independent of heap size (like ZGC).

Example: Enable Shenandoah GC

java -XX:+UseShenandoahGC MyApp

6️⃣ Epsilon Garbage Collector (No GC)

JVM Option: -XX:+UseEpsilonGC
Best for: Performance testing, debugging memory usage
How it works:

  • Does not perform garbage collection.
  • When memory runs out, the JVM crashes.

Example: Enable Epsilon GC

java -XX:+UseEpsilonGC MyApp

3. Comparison of HotSpot Garbage Collectors

GC TypePause TimeThroughputHeap Size SupportUse Case
Serial GCHighLowSmall heaps (<1GB)Single-threaded apps
Parallel GCMediumHighLarge heapsMulti-threaded apps
G1 GCLowMediumLarge heaps (>4GB)General-purpose
ZGCUltra-lowMediumHuge heaps (16TB)Low-latency apps
Shenandoah GCUltra-lowMediumLarge heapsUltra-low pause times
Epsilon GCNo GCMax throughputAnyTesting only

4. How to Choose the Right GC?

Small, Single-threaded App? → Use Serial GC
Multi-threaded, Large Heap? → Use Parallel GC
Balanced Performance? → Use G1 GC (default)
Low-latency App? → Use ZGC or Shenandoah
Performance Testing? → Use Epsilon GC

This entry was posted in Без рубрики. Bookmark the permalink.