Sub topic 3
Reachability: What the Garbage Collector Keeps
This chapter assumes you know JavaScript variables, objects, and functions, and that you have met the idea of scope. It builds on the previous chapters, which covered the allocate-use-release lifecycle and the difference between primitives and references.
JavaScript gives you no free() call. The engine decides when memory can be released, and it decides by one rule: an object stays alive while it is reachable. A reachable object is one your program could still get to by following references from a fixed set of starting points.
Roots: where reachability starts
A root is a value the engine treats as alive no matter what. Common roots include:
| Root | Example |
|---|---|
| Global object | Properties of `globalThis`, such as `window.app` |
| Current call stack | Local variables and parameters of functions that are running |
| Active closures' scopes | Variables captured by functions that are themselves reachable |
| Engine and host internals | Pending timers, registered event listeners, resolved module bindings |
Any object you can reach from a root by following property references, array elements, or captured variables counts as reachable. Everything else is garbage. That includes objects that still point at each other.
Watching reachability change
What counts is whether a path exists from a root. How many variables once held the object makes no difference.
let user = { name: "Ada" };
let admin = user;
user = null; // the object is still reachable through admin
console.log(admin.name); // "Ada"
admin = null; // now unreachable and eligible for collectionAfter user = null, the object still has one path to a root, through admin. After admin = null, no path remains. The object becomes eligible for collection. The engine frees it at some later point that you don't control.
Reachability is transitive
A single reachable reference keeps a whole graph alive:
const cache = {
current: {
items: [{ id: 1, payload: new Array(100_000).fill(0) }],
},
};
// Everything under cache is reachable from the module scope.
cache.current = null;
// The inner object, the array, and the 100,000-element payload
// all become unreachable together.This works in both directions. Detaching a parent can free a large subtree in one step. One stray reference to a small object can also pin a large structure, because that small object may point into the rest of the graph.
Common mistakes
- Expecting
nullto free memory right away. Setting a variable tonullonly removes one reference. If another path exists, the object survives. If none exists, it is freed later, not at once. - Expecting
deleteto free memory.delete obj.propremoves a property. The value is freed only if that property was its last path from a root. - Forgetting hidden roots. Timers, listeners, and closures make references that do not appear as plain variables in your code. Later chapters cover each of these.
- Worrying about cycles. Objects that reference each other but cannot be reached from a root are collected. The next two chapters explain why older engines got this wrong and modern ones get it right.
Exercise
After the code below runs, which of the three objects are eligible for collection? let a = { v: 1 }; let b = { v: 2, link: a }; let c = { v: 3 }; a = null; c = b;
Show answer
Only the object originally in c ({ v: 3 }) is eligible. The { v: 2 } object is reachable through both b and c, and the { v: 1 } object is still reachable through b.link, even though the variable a no longer refers to it.
Exercise
A module-level array called log receives a new entry object on every request and is never cleared. Each entry is read once and never touched again. Is this a leak under the reachability rule, and why?
Show answer
Yes. Each entry stays reachable from a root (the module scope, through log), so the collector must keep it, even though the program never uses it again. Reachable but unneeded objects are what a leak is.