What are the different types of garbage collectors in Java?
Java provides several garbage collector implementations, each representing a different set of tradeoffs between throughput, latency, and resource usage.
Serial GC (-XX:+UseSerialGC) uses a single thread for all GC work and Stop-the-World pauses for both Minor and Major collection. It is appropriate for small heaps (< 100 MB) and client applications or containers with a single CPU.
Parallel GC (-XX:+UseParallelGC, default before Java 9) uses multiple threads for GC work, maximizing throughput. It still uses Stop-the-World pauses but parallelizes the collection work. Best for batch processing jobs where throughput matters more than latency.
CMS (Concurrent Mark Sweep, -XX:+UseConcMarkSweepGC) performed most marking and sweeping concurrently with application threads, reducing pause times. However it did not compact the heap, leading to fragmentation. It was deprecated in Java 9 and removed in Java 14.
G1GC (Garbage First, default since Java 9) divides the heap into equal-sized regions rather than contiguous Young/Old areas. It prioritizes regions with the most garbage (hence 'Garbage First'), performs concurrent marking, and compacts incrementally during mixed collections. It targets a configurable pause time goal (-XX:MaxGCPauseMillis). G1 is the best general-purpose choice for heaps between 6 GB and 32 GB with mixed throughput/latency requirements.
ZGC (-XX:+UseZGC, production-ready since Java 15) performs marking, relocation, and remapping concurrently using load barriers and multi-colored pointers, keeping Stop-the-World pauses under 1ms regardless of heap size. Optimal for latency-sensitive services with large heaps.
Shenandoah (-XX:+UseShenandoahGC, from Red Hat, available in OpenJDK) similarly achieves concurrent compaction using a Brooks forwarding pointer indirection, with pause times competitive with ZGC. Best for applications that cannot afford long GC pauses but are running on commodity JVMs without ZGC availability.
| Collector | Pause Style | Compaction | Best For | Default Since |
|---|---|---|---|---|
| Serial | Stop-the-World (single thread) | Yes | Small heaps, single-CPU | — |
| Parallel | Stop-the-World (multi-thread) | Yes | Batch / throughput-focused | Java 8 default |
| CMS | Mostly concurrent | No (fragmentation risk) | Low-latency (deprecated) | Removed Java 14 |
| G1GC | Concurrent + short STW | Incremental | General-purpose, 6–32 GB heaps | Java 9+ |
| ZGC | Concurrent (< 1ms STW) | Concurrent | Large heaps, latency-critical | Java 15+ |
| Shenandoah | Concurrent (< 1ms STW) | Concurrent | Low-latency on standard JVM | OpenJDK option |
Names at least four collectors with correct throughput/latency characterizations and knows G1GC is the Java 9+ default.
Accurately describes the mechanism of each collector (region-based for G1, colored pointers for ZGC, Brooks pointers for Shenandoah), explains when to choose each, and notes CMS deprecation/removal.
Reading the answer is step one. Explaining it unprompted — under interview pressure — is what actually matters. Get AI-graded feedback on your answer with follow-up probes on your weak points.
Get Graded — Free Assessment