Robot Memory Recycler
The factory memory floor is packed with old robot parts. Trace what the control console can still reach, recycle abandoned objects, and uncover a reference hiding inside an alarm listener.
π§ Before we start
Pizza Saga Kitchen tracked which steps were still active before undoing work. Programs also track which objects are still active. Garbage collection starts at trusted roots, follows references, marks everything reachable, then sweeps away what no running part can reach.
Build the collector cycle
Arrange the memory phases in order. Objects must exist before references connect them; a released reference may make an object unreachable; collection then finds and recycles it.
Trace from the root
Mark only objects reachable from the control console. Follow each arrow outward; disconnected parts are not alive just because they still occupy memory.
ROOT
Sweep only the unreachable parts
The mark phase ended. Two objects have no path from any root. Select both orange objects for the recycler, then sweep once.
MARKED
MARKED
UNMARKED
UNMARKED
Debug a hidden memory leak
A retired diagnostic panel still refuses to disappear. The alarm system keeps a listener that points to it, creating a hidden path from a root. Break the correct reference before collecting.
ROOT
unused, but reachable
π What you learned
- Objects stay alive while a program can reach them through references.
- Garbage collectors begin from roots such as active program controls.
- The mark phase follows references and records every reachable object.
- The sweep phase reclaims unmarked objects while preserving marked ones.
- Removing a visible object is not enough if a hidden listener still references it.
- Detaching unused listeners prevents memory leaks without destroying useful roots.