Garbage Collection
Garbage collection reclaims heap objects that your program can no longer reach. You do not free memory by hand; you drop references and the collector notices.
Detailed Explanation
A value is reachable if you can get to it from a root by following properties, array slots, closures, and pending callbacks. Roots include the current stack, module and global bindings, and objects the host is still using (a registered listener, a live timeout, a DOM node still in the tree). Unreachable objects are garbage. The collector reclaims them at a time it chooses.
JavaScript engines (V8, JavaScriptCore, SpiderMonkey) use generational, mostly mark-and-sweep / mark-compact collectors, plus a nursery for new objects. You do not pick the algorithm in application code. You shape the object graph so garbage becomes unreachable quickly.
There is no portable gc() you should call in production. V8 exposes global.gc() only when Node is started with --expose-gc, for tests and benchmarks. WeakRef and FinalizationRegistry let you observe collection without pinning the object; they are not a destructor API and they do not run promptly.
Allocation is the other half. Every { }, every [], every closure, every promise is a potential GC tax. Hot loops that allocate per iteration (.map in an animation frame, a new function per element) create nursery traffic. Reuse buffers and typed arrays when you have measured a problem.
What GC does not do: close sockets, remove DOM listeners, or abort fetches. Those are external resources. If a dead object still has a listener on a live object, the dead object is not garbage. That is a leak.
What it costs you: pauses and hidden retention. A full heap mark walks the live graph; huge graphs mean longer pauses (or more concurrent marking work). The programmer cost is thinking reachability is "I stopped using this variable" when a cache, a listener, or a detached subtree still points at it.
Reach for GC literacy when a heap snapshot grows across navigations, when Node's heapUsed trends up, or when you design a cache (you need eviction, not hope). Reach for WeakMap when the lifetime should follow someone else's object.
Code Examples
function ephemeral() {
const tmp = { n: 1 }; // becomes unreachable when ephemeral returns
return tmp.n;
}
console.log(ephemeral()); // => 1const cache = new Map();
function remember(key, value) {
cache.set(key, value); // reachable until cache.delete or cache.clear
}
remember("user:7", { email: "ada@example.com" });
cache.delete("user:7"); // value can be collected after thisNode only, and only with --expose-gc
// Node only, and only with --expose-gc
if (typeof globalThis.gc === "function") {
globalThis.gc();
}Common Gotchas
Dropping a local is not enough if a live listener or cache still points at the object
fix the graph, not "call GC".
Closures retain the whole environment
a tiny callback that closes over `huge` keeps `huge` alive.
You cannot rely on finalizers for correctness
`FinalizationRegistry` callbacks are optional and delayed. Release sockets in `finally` / `close`.
`delete obj.key` does not free `obj`
it may help a property become garbage; `obj` lives until nothing references it.