HomeComputer ScienceMark-and-Sweep Garbage Collector

Mark-and-Sweep Garbage Collector (2D)

Interactive 2D mark-and-sweep garbage collector: allocate heap objects on a node-graph, wire and drop references (including reference cycles), then watch a real BFS reachability mark from the roots followed by a sweep phase — and see why naive reference counting can never collect an unreachable cycle.

Computer Science2DModerate60 FPS📱 Mobile-adapted⇄ 3D version
2d-machine-learning-computer-science-2 ↗ Open standalone

Every managed-memory language — Java, JavaScript, Python, Go — reclaims memory automatically by treating the heap as a directed reference graph and asking one question: can this object still be reached from a root? This 2D companion renders that heap as a live node-and-edge graph. Allocate objects, wire and drop references, deliberately build a reference cycle with no path from any root, then watch a real breadth-first mark phase spread outward from the gold root nodes and a sweep phase eject every node the wavefront never touched — including the cycle. Flip on reference-counting mode to see the classic failure mode play out on the exact same graph: local pointer counts never fall to zero for a cycle, so the naive collector leaks it forever.

⚙ Under the hood

Watch a real mark-and-sweep garbage collector at work on a live 3D heap: allocate objects, wire and drop references, then trigger a BFS reachability scan from the roots followed by a sweep that reclaims everything the wavefront never touched, with an optional generational young/old mode.

garbage collectionmemory managementcomputer sciencegraph algorithmsmark and sweepcompilers

2D · HTML5 Canvas 2D · 60 FPS target · runs fully client-side, no install

What did you find?

Add reproduction steps (optional)