Garbage Collection
Free heap memory automatically by identifying objects no longer reachable from GC roots — and choose the GC algorithm that fits your latency vs throughput target.
Garbage collection frees Java programmers from manual memory management by automatically reclaiming objects that can no longer be reached. The core idea is reachability: starting from a set of GC roots — local variables on thread stacks, static fields, JNI references — the collector traces every object reachable by following references. Anything not reached is unreachable and its memory can be reclaimed. Crucially, this is about reachability, not reference counting, which is why a self-referential island of objects with no path from a root is still collected.
A janitor that throws out anything no longer connected to the main building (GC roots). G1 cleans one room at a time during business hours. ZGC works alongside everyone without disturbing them.