☕ Java Advanced

What are the main garbage collection algorithms in Java?

Answer

Java offers several GC algorithms for different use cases. Serial GC uses a single thread — suitable only for small apps with small heaps. Parallel GC uses multiple threads for minor/major collections — good throughput but pauses. G1 (Garbage-First) GC (default since Java 9) divides the heap into equal-sized regions, prioritizing collection of regions with most garbage — balances throughput and pause times. ZGC (Java 15+) is designed for ultra-low pause times (<10ms) regardless of heap size — most collection work is done concurrently with the application. Shenandoah (OpenJDK) is similar to ZGC. Choose GC based on requirements: throughput (Parallel), low-latency (ZGC, Shenandoah), or balanced (G1).