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 Collector | JVM Option | Best For |
---|---|---|
Serial GC | -XX:+UseSerialGC | Small applications, single-threaded environments |
Parallel GC (Throughput GC) | -XX:+UseParallelGC | Multi-threaded applications, high throughput |
G1 (Garbage First) GC | -XX:+UseG1GC (Default in Java 9+) | General-purpose, large heaps |
ZGC (Low-Latency GC) | -XX:+UseZGC | Low-latency, real-time applications |
Shenandoah GC | -XX:+UseShenandoahGC | Ultra-low pause time applications |
Epsilon GC (No GC) | -XX:+UseEpsilonGC | Performance 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 Type | Pause Time | Throughput | Heap Size Support | Use Case |
---|---|---|---|---|
Serial GC | High | Low | Small heaps (<1GB) | Single-threaded apps |
Parallel GC | Medium | High | Large heaps | Multi-threaded apps |
G1 GC | Low | Medium | Large heaps (>4GB) | General-purpose |
ZGC | Ultra-low | Medium | Huge heaps (16TB) | Low-latency apps |
Shenandoah GC | Ultra-low | Medium | Large heaps | Ultra-low pause times |
Epsilon GC | No GC | Max throughput | Any | Testing 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