/ THE IDEA
A stored record is called an object; a reference is a stored route to one. Collectors commonly begin with roots, starting points such as active instructions and program-wide variables, then follow references. Objects reachable through those routes are kept. Others can be reclaimed. This rule is about reachability, not usefulness. A hidden screen’s event listener, code registered to respond to an action, can still refer to a large image collection. If the listener remains registered somewhere reachable, the collection can survive too, even though the user has finished with that screen.
THE FORMAL IDEA
roots → reachable references → retained objects
| Roots are starting points the runtime treats as live. | | Reachable means some chain of references connects a root to the object. A memory leak in a collected language often means accidentally keeping such a chain, rather than forgetting a manual free instruction. |
|
RUN THE TINY EXAMPLE
Change one thing. Predict what happens.
Root R points to listener L; L points to image collection A. Closing the visual panel leaves R → L → A unchanged → A is retained. Change the state by unregistering L and removing all other references to A. Now no root reaches A → the collector may reclaim it.
|
The meaningful input change is removal of the last live reference chain. Removing only the visible panel did not satisfy that condition. Collection timing can still be later than the moment the chain disappears.
/ SO WHAT?
This makes memory investigation a question about ownership and lifetime. Ask who still refers to an object and how long that owner lives. A long-lived cache might deliberately keep data; an accidentally retained screen might not. Both appear reachable, so the collector cannot infer the programmer’s intention.
ONE CAVEAT |
| Collectors differ and can pause or run incrementally. Reclaimed memory need not immediately reduce the operating system’s process total, and native resources may need explicit cleanup. |
KEEP THIS
Automatic cleanup can reclaim unreachable objects, but it cannot know that reachable objects have become useless to you.
|
NEXT: Why a small pressure change can move a large load
|