Java FAQ: Top Questions
56. What are the different garbage collectors in Java?
Java’s garbage collectors (GCs) in the JVM automatically reclaim memory by removing unreachable objects, with different algorithms for performance trade-offs.
-
Serial GC:
Single-threaded, for small applications (
-XX:+UseSerialGC
). -
Parallel GC:
Multi-threaded, throughput-focused, default in Java 8 (
-XX:+UseParallelGC
). -
CMS (Concurrent Mark-Sweep):
Low-pause, concurrent, deprecated in Java 14 (
-XX:+UseConcMarkSweepGC
). -
G1 GC:
Region-based, low-latency, default since Java 9 (
-XX:+UseG1GC
). -
ZGC:
Ultra-low-pause, scalable, experimental in Java 11, production in Java 15 (
-XX:+UseZGC
). -
Shenandoah:
Low-pause, concurrent, introduced in Java 12 (
-XX:+UseShenandoahGC
).