intermediate

Garbage collection

Reachability, roots, mark-and-sweep mental model, finalization limits, and memory pressure.

JavaScript uses automatic memory management. Collectors free objects that are no longer reachable from roots (global object, current stack, closures in scope).

If any reference path remains — closure, cache, listener, timer, DOM node, global — the object stays alive. Setting a local variable to `null` rarely fixes a leak if another path retains it.

`FinalizationRegistry` is not a reliable business-logic cleanup mechanism.

On interviews, explain the concept with a concrete example and name the runtime behavior interviewers probe.

Common pitfalls include mixing similar APIs and forgetting edge cases during live coding.

The trade-off is often clarity versus performance or safety versus convenience.

Checklist:

  • Reason from roots and reachability.
  • Find retaining references in DevTools.
  • Clean listeners and timers on teardown.