Popular Garbage Collection Implementations in Java

Popular garbage collection implementations in Java, including Serial, Parallel, CMS, G1, Shenandoah, and ZGC, with insights into their performance and use cases.

Java garbage collection is one of those topics developers either love or avoid. I fall into the latter camp until something goes wrong—then I suddenly care a lot about pause times and throughput. Here is a practical look at the most common GC implementations, when to use them, and what to watch out for.

Serial GC

Serial GC is the simplest. It runs everything in a single thread, which means no thread coordination overhead. For small heaps and single-core machines, this is actually faster than the fancy alternatives. The tradeoff is stop-the-world pauses—your application threads stop while GC runs. On a heap larger than a few hundred megabytes, those pauses get painful.

Enable with -XX:+UseSerialGC. I use this for local development containers and small CLI tools where simplicity matters more than throughput.

Parallel GC

Parallel GC (also called the Throughput Collector) adds multiple GC threads. It still does stop-the-world pauses, but shorter ones because the work is parallelized. Throughput is the goal here—maximize the time your application spends doing useful work, minimize the time spent collecting garbage.

I see this as the default for batch processing, data pipelines, and anything where latency matters less than throughput. Tune thread count with -XX:ParallelGCThreads.

CMS — Deprecated but Worth Mentioning

Concurrent Mark-Sweep was the go-to low-latency collector for years. It does most work concurrently with application threads, avoiding long pauses. The problem: fragmentation. Without compaction, the heap gradually fragments, and eventually a full GC kicks in—which is a stop-the-world event anyway.

CMS is deprecated since Java 14. If you are still on it, migrate to G1, Shenandoah, or ZGC.

G1 GC

G1 (Garbage-First) divides the heap into regions and collects the ones with the most garbage first. It balances throughput and latency, and it has been the default GC since Java 9. For heaps between 4GB and 16GB, G1 is usually the right choice.

I have tuned G1 for production services with 8GB heaps and 200ms pause-time goals. It works well if you give it enough headroom—-XX:MaxGCPauseMillis is a goal, not a guarantee.

Shenandoah

Shenandoah does concurrent compaction, which is the key difference from G1. Pause times stay low even on large heaps because most work happens concurrently. The cost is higher CPU usage during normal operation.

For latency-sensitive services—API gateways, real-time systems—Shenandoah is worth the CPU overhead. It production-ready since Java 12.

ZGC

ZGC targets sub-millisecond pauses regardless of heap size. It uses colored pointers and load barriers, which is more complex under the hood but invisible to the application. Heaps up to terabytes are supported.

I have not run ZGC in production yet, but for financial systems or high-frequency trading where every millisecond counts, it is the one to watch. Memory overhead is higher—expect 15-20% more heap than G1.

Choosing and Tuning

The decision matrix is straightforward:

  • Small heap, single thread: Serial
  • Throughput-focused, multi-core: Parallel
  • Balanced, moderate latency: G1
  • Low latency, large heap: Shenandoah or ZGC

Key flags: -Xmx, -Xms, -XX:MaxGCPauseMillis. Monitor with VisualVM or JFR. If you are not monitoring GC, you are flying blind.