Begin with a graph of references
Suppose an array is assigned to a variable and the program stops using that variable. Does its storage immediately become available? A source-level name alone cannot answer this. We need to ask which runtime references can still lead to the array. A collector does not generally decide whether its contents will be useful to the application. It uses the runtime's reference model to identify storage that may be reclaimed.
Our starting model is a paused graph: objects are vertices and references are directed edges. Roots are starting references recognized by the runtime, such as references held by active frames or static storage. Their exact definition depends on the implementation. State the root set before tracing. Keep this graph model separate from implementation optimizations and concurrent execution. If you have studied ownership, shift the question from who is responsible for release to which paths retain a value.
Changing roots changes the live set
Let the heap contain a, b, c, d, and e. Object a refers to b and c, b refers to c, c refers to a, and d and e refer to each other. With only a as a root, tracing reaches a, b, and c. The d–e cycle has references, but no path enters it from a root. It is collectible in this model because it is unreachable, not because its members have no incoming references.
Add d to the roots without changing any edges. All five objects now belong to the live set. Remove a instead and only d and e remain live. This also explains why “GC cannot collect cycles” is too broad. A tracing collector can find unreachable cycles. Simple reference counting needs additional treatment when objects retain each other. Name the mechanism before attributing a limitation to garbage collection as a whole.
| Roots | Reachable set | Unreachable cycle |
|---|---|---|
| a | a, b, c | d, e |
| a, d | a, b, c, d, e | None |
| d | d, e | a, b, c |
Trace first, reclaim afterward
A marking pass keeps a worklist of references to inspect and a set of objects already visited. Start with the roots. Remove an item, mark it if unseen, and add its outgoing references. Do not expand an already visited object again. This prevents a cycle from causing an infinite traversal. When the worklist is empty, the marked set contains all objects reachable in this graph.
Reclamation is a separate step that consumes this result. Our model removes heap entries outside the marked set. Do not delete an unvisited object halfway through tracing: another pending branch may still reach it. Visiting each vertex once does not eliminate the cost of examining edges or scanning heap entries. Reason about both vertices and edges when estimating work. A compact implementation is not evidence of a fixed pause time or constant collection cost.
A retained cache can keep useless data alive
Extend the graph with a rooted cache that refers to a large payload. Even if the application will never use that payload again, the collector in our model cannot reclaim it while the cache retains the reference. Collection has not malfunctioned. The retention relationship expressed by the program differs from the lifetime intended by its author.
Investigate growth by asking which root reaches the object and through which holders. Unbounded cache keys, forgotten event subscriptions, and lists of completed requests can all provide retention paths. Possible changes include limits, expiry, deregistration, and removing obsolete references. Weak references require their own policy: when may a needed value disappear, and who recreates it? Merely replacing a strong reference with a weak one does not define correct cache behavior or make recomputation safe.
Separate memory from external resources
An unreachable wrapper object does not guarantee that its file descriptor is closed by a required deadline. Collection scheduling, finalization behavior, and ordering depend on the language and runtime contract. Conditions for reclaiming memory are different from conditions for returning a file, socket, or lock.
If a file must close even when an operation raises an exception, use the language's scoped resource mechanism, such as Python's with statement or Java's try-with-resources. A runtime may offer finalization as a supplementary facility; that does not make it a reliable schedule for ordinary cleanup. Objects retained through caches or cycles may exhaust descriptor limits before memory becomes the obvious problem. During review, identify acquisition, normal exit, exceptional exit, and responsibility after transfer. “There is a collector” cannot answer those resource-lifetime questions.
Real collectors coordinate with a changing heap
Our graph stays fixed during traversal. Real applications allocate objects and update references. If a new edge is added after the collector has inspected its source, the implementation needs a rule for maintaining a valid view of reachability. Depending on its design, it may coordinate through pauses, write barriers, or records of references between generations. The small graph model implements none of that coordination.
Some collectors also move surviving objects to consolidate storage. References must then be updated or represented in a way that remains valid across movement. A pointer passed to external C code may require a pinning or handle contract if the runtime cannot track that external reference. This is a useful connection to FFI: crossing a boundary raises questions about movement and untracked users as well as about whether storage is collectible.
Define the memory metric before measuring
Before evaluating a collector change, specify what “less memory” means. Live heap bytes, committed heap capacity, and process resident memory are different quantities. Reclaiming unreachable objects need not immediately reduce the value reported by the operating system: the runtime may retain storage for subsequent allocation. A flat result in a tiny experiment also cannot exclude retention problems over a long-running workload.
Record the runtime version, collector, heap settings, input, and measurement interval. For latency, inspect distributions and pauses rather than relying only on an average. The allocation experiment measures bytes allocated by a thread. That counter is distinct from the size of the reachable set discussed here. Relate each metric to the condition you changed. Keeping these meanings explicit makes performance and memory explanations more reproducible.
Exercise: explain the retaining paths
Return to the original graph with a as its only root. Add an edge from c to d. The answer is now all five objects: a reaches c, c reaches d, and d reaches e. Next remove only the direct edge from a to c. The live set remains unchanged because a still reaches c through b. Finally remove the cache root from the extended example. Both cache and payload become collectible if no other path reaches either one.
Write down the roots and the paths that remain or disappear, not just a final set. The companion experiment expresses the same reasoning as a Python graph and uses assertions for sharing, cycles, and termination. It establishes a basis for explaining retention. It does not measure the pause behavior or throughput of a production collector.