Mark and Sweep
Mark-and-sweep is a garbage-collection algorithm: mark every object reachable from roots, then sweep (reclaim) the objects that were not marked. JavaScript engines use this idea, usually with generations and extra tricks.
Detailed Explanation
Reference counting (used in CPython alongside a cycle collector) frees an object when its count hits zero. Cycles (a.b = a) never hit zero. Mark-and-sweep does not count. It walks.
Mark. Start from roots: stack slots, globals, handles the host holds. Follow every pointer: object properties, array elements, closure environments, promise reactions. Each visited object is marked live. Anything not marked is unreachable, even if objects point at each other.
Sweep. Walk the heap (or a free-list / page list) and reclaim unmarked objects. A compacting collector also slides live objects together so allocation is a bump pointer again and fragmentation drops. Moving objects requires updating pointers; engines hide that from you.
JavaScript VMs add generations. Most objects die young. The nursery is collected often with a cheap mark of a small space. Objects that survive are promoted to the old generation, which is marked less often, sometimes concurrently or incrementally so a tab does not freeze for tens of milliseconds.
You cannot implement mark-and-sweep in user JavaScript for language values; the engine owns the heap. You can apply the idea to your own graphs: a game scene graph with a "mark this frame" flag is the same algorithm.
Why the name shows up in interviews: to contrast with reference counting, to explain why cycles are fine (const a = {}; a.self = a is still collectable once a is unreachable from roots), and to explain pause times (mark visits the live set; a huge live set is a huge mark).
What it costs you: mark time proportional to live data, not garbage. A leak that keeps a million objects live makes every old-space collection slower. Killing garbage is cheap; retaining junk is expensive.
Reach for this model when a heap snapshot is "why is this still here?" — find the path from a root. That path is the mark graph. Break it.
Code Examples
A cycle is collectable once no root points at it.
// A cycle is collectable once no root points at it.
function cycle() {
const a = {};
const b = {};
a.other = b;
b.other = a;
return 1; // a and b are unmarked after return
}
console.log(cycle()); // => 1A "root" you forgot: a module-level set keeps the cycle alive.
// A "root" you forgot: a module-level set keeps the cycle alive.
const roots = new Set();
function leak() {
const a = { name: "stuck" };
a.self = a;
roots.add(a);
}
leak();
console.log(roots.size); // => 1 (still marked from the module binding `roots`)
roots.clear();Common Gotchas
Cycles are not leaks by themselves
a leak is a path from a root. The cycle only makes the retained set larger once rooted.
Live-set size drives mark cost
caches without eviction keep old-space large and collections slow.
You will not see a `mark()` API
DevTools heap snapshots *are* a mark from roots. Use them.
Reference counting is not how JS engines reclaim objects
do not "balance" retain counts in application JS.