Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Memory Management and DevTools  ›  Lesson

Garbage Collection

Garbage Collection 10 minJavaScript's garbage collector reclaims memory for objects that are no longer reachable from a root (like a global or an active function's variables) through any chain of references; a WeakMap or WeakSet holds its keys weakly, so they don't count as a reference that keeps an object reachable, which is exactly why they exist.

Garbage Collection

In short: JavaScript's garbage collector reclaims memory for anything that is no longer reachable — not tied, by any chain of references, back to a root like a global variable or a currently-running function's local variables. Reachable, however many steps away, means kept; unreachable means eligible for collection. You can never observe or force the exact moment collection happens — only reachability is something your code actually controls.

A room with a door (reachable roots): a string ties the door to session, and another string ties session to session.user — both stay; a separate "old cart" box has no string reaching it from the door, so it's swept out eventually; a note that reachable means kept however many steps away, unreachable means eligible for collection on the engine's own schedule

Reachable through a chain, however long

An object stays alive as long as something reachable points to it — directly, or through other objects:

javascript

Setting session to null doesn't make { name: "Asha" } unreachable — savedUser still points straight at it. An object needs every path to it cut before it becomes eligible for collection, not just one.

WeakMap: a reference that doesn't count

Sometimes you want to attach data to an object without that attachment itself keeping the object alive forever. That's what WeakMap is for — verified here only by what you can observe (the entry exists while the key does), never by timing:

javascript

A regular Map would hold a strong reference to obj as a key — the Map itself would keep obj reachable forever, even if nothing else in the program still needed it. WeakMap holds its keys weakly: that reference doesn't count toward reachability. Once every other reference to obj is gone, it becomes eligible for collection — and its entry in the WeakMap disappears along with it. Exactly when that happens is decided by the engine; nothing here claims a specific moment, because that part genuinely isn't observable from code.

Quick practice

quick drill

What makes an object eligible for garbage collection?

quick drill

In the session/savedUser example, why does savedUser.name still work after session = null?

quick drill

Why does a WeakMap's key not keep its corresponding object alive forever?

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainWhy a WeakMap key can disappear

Explain the difference between const cache = new Map(); cache.set(obj, data); and const cache = new WeakMap(); cache.set(obj, data); in terms of what keeps obj reachable, once every other variable pointing at obj is set to null.