Last Updated: June 6, 2026
Garbage collection is the part of the JVM that frees memory you don't need anymore, so you don't have to. In languages like C, every byte you allocate with malloc you have to release with free, and if you forget, you leak; if you free too early, you crash. Java made a different bet: the runtime walks your live objects, figures out which ones are still reachable, and reclaims the rest. This chapter is about how that reclamation works in principle, why reachability is the rule, what "stop the world" actually means, and why System.gc() is closer to a polite suggestion than a command.
The previous two chapters covered how Java lays out memory: the stack for frames and local variables, the heap for objects, and the metaspace for class metadata. Garbage collection lives on the heap. The stack cleans itself up when a method returns; the heap doesn't, which is why it needs a collector. Subsequent chapters drill into specific collectors (Serial, Parallel, G1, ZGC) and tuning, but the ideas here apply to all of them.