Managing object lifetime
Release references to completed application data so it can become collectible.
JS Puzzles · Const
JavaScript automatically reclaims objects that are no longer reachable from roots. The page explains reachability and the mark-and-sweep algorithm, including how cycles can be collected.
JavaScript automatically reclaims objects that can no longer be reached from any root, such as global variables or currently executing functions. The usual mark-and-sweep process marks roots and everything reachable through them, then removes unmarked objects. Cycles are handled because reachability, not merely reference counting, determines whether objects remain.
let user = { name: "Alice" };
user = null; // The object is no longer reachable
// The garbage collector can reclaim the object automatically.After the only reference to the object is removed, the object becomes unreachable and may later be reclaimed automatically.
Release references to completed application data so it can become collectible.
Diagnose memory retention by tracing references from long-lived roots.
Understand why cyclic object references do not necessarily leak memory.
ReachabilityMark-and-sweepRootsMemory management