Garbage Collection Algorithms in the JVM
The JVM provides multiple Garbage Collection (GC) algorithms to efficiently manage heap memory by reclaiming space from objects that are no longer needed.
1️⃣ Serial Garbage Collector
Algorithm
- Uses a single thread for both minor and major garbage collection.
- Stops the world (pauses all application threads) during GC.
- Uses a copying algorithm in the Young Generation (objects moved from Eden to Survivor spaces).
- Uses Mark-Sweep-Compact in the Old Generation.
Best For
✔ Single-threaded applications.
✔ Small applications (e.g., desktop apps).
Drawbacks
❌ Long GC pauses (not suitable for multi-threaded applications).
Enable With
-XX:+UseSerialGC
2️⃣ Parallel Garbage Collector (Throughput GC)
Algorithm
- Uses multiple threads to speed up garbage collection.
- Still stops the world during GC, but performs GC in parallel.
- Uses copying collection for Young Generation and Mark-Sweep-Compact for Old Generation.
Best For
✔ Multi-threaded applications that prioritize throughput over low-latency.
✔ Applications running on multi-core machines.
Drawbacks
❌ Still has full GC pauses, which may not be ideal for low-latency applications.
Enable With
-XX:+UseParallelGC
3️⃣ Concurrent Mark-Sweep (CMS) Garbage Collector
Algorithm
- Reduces GC pause times by performing most work concurrently with application threads.
- Uses a Mark-Sweep algorithm for the Old Generation (no compacting step).
- Phases:
- Initial Mark – Identifies root references (Stop-the-world pause).
- Concurrent Mark – Finds reachable objects concurrently.
- Remark – Handles objects missed in concurrent marking (Stop-the-world pause).
- Concurrent Sweep – Frees unreachable objects concurrently.
Best For
✔ Low-latency applications (e.g., financial systems, real-time applications).
Drawbacks
❌ Can cause fragmentation (no compaction step).
❌ Uses more CPU than other collectors.
Enable With
-XX:+UseConcMarkSweepGC
Deprecated from Java 9 and removed in Java 14. Use G1GC or ZGC instead.
4️⃣ Garbage First (G1) Garbage Collector
Algorithm
- Divides heap into regions (instead of separate generations).
- Uses a priority-based approach (focuses on regions with most garbage).
- Performs mostly concurrent collection with shorter pauses.
- Includes compaction step (avoids fragmentation).
Phases:
- Young GC – Collects Eden and Survivor spaces.
- Mixed GC – Collects both Young and Old generations.
- Full GC (rare) – Stops the world if necessary.
Best For
✔ Large heap applications (e.g., microservices, enterprise applications).
✔ Balanced performance (good throughput with low GC pause times).
Drawbacks
❌ Slightly higher CPU overhead than Parallel GC.
Enable With
-XX:+UseG1GC
Default GC since Java 9.
5️⃣ Z Garbage Collector (ZGC)
Algorithm
- Ultra-low latency collector.
- Performs most of the GC work concurrently (without stopping the application).
- Only short Stop-the-world pauses (few milliseconds).
- Uses region-based allocation similar to G1 but focuses on concurrent compaction.
Best For
✔ Applications that require very low latency (sub-10ms GC pauses).
✔ Large heaps (supports up to 16TB of heap).
Drawbacks
❌ Higher CPU overhead compared to G1.
❌ Not ideal for small heaps (<4GB).
Enable With
-XX:+UseZGC
Available since Java 11, stable from Java 15.
6️⃣ Shenandoah Garbage Collector
Algorithm
- Another low-latency GC, like ZGC.
- Performs concurrent compaction, preventing fragmentation.
- Pause times are independent of heap size.
Best For
✔ Real-time systems where consistent low pause times are critical.
Drawbacks
❌ High CPU usage.
❌ Not the best for throughput-focused applications.
Enable With
-XX:+UseShenandoahGC
Introduced in Java 12, stable in Java 15.
7️⃣ Epsilon Garbage Collector (No GC)
Algorithm
- Does nothing (no garbage collection).
- Objects keep accumulating until memory runs out.
Best For
✔ Performance testing and benchmarking.
✔ Environments where manual memory management is used.
Drawbacks
❌ Will run out of memory if objects are not explicitly managed.
Enable With
-XX:+UseEpsilonGC
Introduced in Java 11.
Comparison Table of GC Algorithms
GC Type | Algorithm | Pause Time | Best For | Enable With |
---|---|---|---|---|
Serial GC | Single-threaded, Stop-the-world | High | Small applications | -XX:+UseSerialGC |
Parallel GC | Multi-threaded Stop-the-world | High | High throughput | -XX:+UseParallelGC |
CMS GC | Concurrent Mark-Sweep | Low | Low-latency apps (deprecated) | -XX:+UseConcMarkSweepGC |
G1 GC | Region-based, concurrent | Medium | Large apps with balanced performance | -XX:+UseG1GC |
ZGC | Fully concurrent, region-based | Very Low | Large heaps, low-latency apps | -XX:+UseZGC |
Shenandoah GC | Concurrent compaction | Very Low | Real-time, consistent low pauses | -XX:+UseShenandoahGC |
Epsilon GC | No garbage collection | None | Testing, benchmarking | -XX:+UseEpsilonGC |
Conclusion
- Serial GC – Simple, good for small applications.
- Parallel GC – High throughput, good for batch processing.
- CMS GC – Low-latency, deprecated (use G1 instead).
- G1 GC – Balanced, default since Java 9.
- ZGC – Ultra-low-latency, great for large heaps.
- Shenandoah GC – Low-latency, alternative to ZGC.
- Epsilon GC – No GC, for testing purposes.
For modern applications, G1GC is the default choice, while ZGC and Shenandoah are best for low-latency systems. 🚀