Root Live Marked this scan Garbage (unreachable) Leaked cycle (RC mode)

Mark-and-Sweep Garbage Collector (2D)

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.