Reachability14 / 19

GC finds reachable objects

Garbage consists of objects the program can no longer access through references.

The garbage collector (GC) starts from the roots and follows references. The objects it finds are still accessible to the program, so it retains them. Memory occupied by the remaining objects can be released.

The example contains the chain root → scene → player. These objects are accessible only through the connections shown.

The buttons replace a reference with null:

  • Scene → player: the player becomes unreachable; the scene remains accessible.
  • Root → scene: both objects become unreachable.

Removing references

The buttons on the connections replace a reference with null. Initial state:

globalThis.activeScene = { player: { hp: 100 } };
RootglobalThis activeScene: reference to scene

Heap

scene✓ Reachable · retain
player: reference to player
player✓ Reachable · retain
hp: 100
Executed operations · interactive diagram
None yet.

A path leads from the root to scene and, through it, to player. Both objects are reachable.

Reachable: 2 of 2

Interactive diagram. There are no other references to these objects here. Unreachable objects remain visible: V8 decides when their memory is actually released.

Key takeaway

If no reference path from the roots leads to an object, GC can release its memory. The engine decides when that happens.

Check the primary source ↗

V8 · Orinoco GC↗Following references from the roots: reachable objects are retained; unreachable objects can be collected.ECMA-262 · PutValue↗Assigning a new value to an object property. Writing null replaces the reference without deleting the object itself.