Garbage collection automatically reclaims memory occupied by objects no longer reachable by a JavaScript program. Interview discussions focus less on manually freeing memory and more on recognizing references that keep data alive unexpectedly. section 3.12 section 3.4
Closures make reachability practical to reason about. A returned function can retain variables from its lexical scope, so captured data remains available while the function remains reachable. Registering that function as a long-lived event listener can therefore retain large objects and cause progressive memory consumption. section 3.1
A closure retained by an event listener
In Listing 3.3, createHandler() captures largeObject and returns a click handler. Registering the handler keeps the closure and its captured reference alive. A safer lifecycle removes the listener with the same handler reference; clearing largeObject can also release its contents when the handler runs. section 3.1
For an interview-ready explanation:
- Describe garbage collection as reachability-based reclamation rather than arbitrary deletion. section 3.1
- Trace the scope chain to identify which closures retain variables. section 3.1
- Diagnose leaks by checking event listeners, callbacks, and other long-lived references. section 3.1 section 3.4











