The JVM does not rely on a single garbage collection algorithm. Instead, it uses different garbage collection strategies depending on the selected Garbage Collector. These algorithms work together to identify unreachable objects and reclaim memory efficiently.
Key Points: • Mark-Sweep identifies unused objects and removes them from memory. • Mark-Compact moves surviving objects together after cleanup to reduce memory fragmentation. • Copying Algorithm transfers live objects from one memory region to another, improving allocation efficiency. • Generational Garbage Collection divides memory into Young and Old generations based on object lifespan. • Modern collectors such as G1 GC, ZGC, and Shenandoah combine multiple algorithms to optimize performance and reduce pause times.
Example: Most newly created objects are allocated in the Young Generation. If they survive multiple garbage collection cycles, they are promoted to the Old Generation, where they are managed differently to improve efficiency.
Interview Tip: A concise interview answer is:
"JVM uses multiple garbage collection algorithms, including Mark-Sweep, Mark-Compact, Copying, and Generational Collection. Modern collectors such as G1 GC combine these techniques to efficiently reclaim memory while minimizing application pause times."