What are the main garbage collection algorithms in Java?
Why Interviewers Ask This
Senior Java engineers are expected to reason about architecture, performance, and edge cases. This question separates mid-level from senior candidates by testing deep system-level understanding.
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).
Pro Tip
Back up your answer with a specific project or situation. Saying 'In my last Java project, I used this when...' immediately makes your answer more credible and memorable.
Previous
What is the difference between Heap dump and Thread dump in Java?
Next
What is the difference between String.valueOf() and toString() in Java?