An interactive tutorial on JavaScript garbage collection: watch a mark-and-sweep collector walk real object graphs, then find a leak with Chrome DevTools.
A frontend app built by Ananda Rizki. More of them at Labs.
Garbage Collection
Tutorial
A first-principles tutorial on garbage collection in JavaScript. It opens by explaining what memory is, what a reference is, and the three designs anyone has made work — manual free, reference counting, and tracing from roots — so that "reachability, not reference counting" means something before it is asserted. Then seven short programs, each with its object graph drawn out and walked by a real mark-and-sweep collector: a reference cycle that is collected anyway, a closure quietly holding a million-element array, a detached DOM node kept alive by a cache, a Map and a WeakMap that differ by one word, and tri-colour marking interrupted mid-walk to show why the write barrier exists. Every lesson also says why the collector is built that way, what the rule costs you in real code, and how to check the claim yourself. An eighth view drops references entirely and shows the same collector as memory — a nursery, an old generation, promotion after two survivals. A ninth is the part most tutorials skip: how to find a leak in your own application with a heap snapshot, a retainer chain and the three-snapshot technique. A tenth lists the sources: the ECMAScript specification's liveness clause, V8's own posts, Ungar's 1984 paper, and the Node commands that produced the numbers, with their real output. The point is that a leak in JavaScript almost always means "still reachable by accident", and that reachability is a substitute for a question no collector can answer.
Built by Ananda Rizki, a software engineer and designer, one of the small web apps he writes for fun.
Explore more apps on Labs