NOTE

Garbage Collection: Core Idea

Reachability-based reclamation, GC roots, object liveness, and why garbage collection manages memory rather than arbitrary external resources.

JavaCreated Updated 1 min readhistorical

This is a historical learning note and may contain outdated or incomplete understanding.

1. The Problem

A managed runtime must identify heap objects that can no longer affect future execution and reclaim their storage safely.

Modern JVM collectors reason primarily from reachability, not simple reference counts.

2. GC Roots

The collector starts from roots such as active thread-stack references, class/static roots, JNI/native roots, and other VM-defined root sets. Objects reachable through the reference graph are considered live for that collection.

Unreachable objects can eventually be reclaimed.

3. Why Not Reference Counting Alone?

Pure reference counting cannot reclaim a cycle whose objects reference each other but are unreachable from the program roots. Tracing collectors naturally handle such cycles.

4. Memory Is Not Resource Lifetime

GC reclaims managed memory. It is not a replacement for deterministic cleanup of files, sockets, DB connections, locks, or other external resources. Use try-with-resources or another explicit lifecycle mechanism for those.

Loading helpful count