Recolector de basura

Herramienta educativa interactiva con visualización. Mark-sweep / generational GC on a toy heap: roots, marks, and frees.

About this tool

Follow a collection

Choose a full collection or young-only collection, a heap and one root object or no root. Prepare heap creates the complete immutable timeline and shows step 0. The selected root is preserved. In the random preset, choose 8–32 objects and an integer seed from 0 to 4294967295. Count and seed are unused for the fixed two-cycle heap.

Use the exact step field, timeline slider, Back, Next, Start or End to inspect the same snapshots. Playback advances at 1, 3 or 10 steps per real second and stops at the end; playing from the end restarts at step 0. Manual navigation and changing speed pause playback. A hidden view freezes its step and resumes only if it was playing. Navigation retains a paused result; reloading or changing language restores valid settings and waits for Prepare heap.

Full collection

Roots seed a FIFO worklist. Each marking step visits one object, marks it, and queues eligible unvisited targets. After the worklist is empty, sweeping examines every object, keeping marked objects and freeing unmarked objects. An unreachable reference cycle is therefore collected even though its members reference one another.

Conservative young-only collection

Only young objects belong to the collection set. Young roots and all old-to-young references supply entry points. These remembered edges are shown as dashed directed references and listed explicitly. Traversal remains within young objects. Old objects are always retained, including old objects that are globally unreachable; their young targets can therefore survive conservatively as well.

Sweeping frees only unmarked young objects. A separate promotion step changes every surviving young object to old. This deliberately simplified one-survival rule is not a JVM or CLR aging policy. The exact old-to-young edge list here is not a card-table approximation or a reproduction of G1.

Heap examples

The fixed heap has eight objects and references 0→1, 1→2, 2→1, 3→4, 4→5 and 5→4. Objects 6 and 7 have no outgoing references. Objects 0, 3 and 7 are old; the rest are young. A full collection rooted at 0 keeps only 0, 1 and 2. With no roots it frees everything. A young-only collection rooted at 0, or with no roots, retains 0, 1, 2, 3, 4, 5 and 7 and frees 6: the edges 0→1 and 3→4 protect both young cycles.

The random heap uses the deterministic mulberry32 generator. Each object has up to two distinct random target references, including possible self-references. IDs divisible by 3 are old, the others young. The same seed and settings reproduce the same heap and timeline.

Reading the graph

Circles with Y denote young objects; squares with O denote old objects. R identifies a root, a dashed ring means queued, and a solid outer ring identifies the active object. A tick denotes marked; a crossed-out object is freed. Active outgoing references are emphasized. Freed objects and faded historical references remain in place to explain the collection; no compaction is shown. The table records each snapshot's actual generation, stored reference IDs and state. The worklist is displayed in FIFO order.

Scope and cost

This is a small stop-the-world teaching model. No allocations occur during a run, and there are no finalizers, weak references, compaction or concurrent write barriers. The collection algorithm takes O(V + E) work for V objects and E references. Retaining a complete snapshot after each step uses O(V² + VE) space in this teaching timeline. That is not a claim about the memory usage or performance of a real garbage collector.

Sources

Microsoft: garbage collection fundamentals and Oracle: G1 and remembered sets.