A memory leak occurs when objects or resources remain retained after an application no longer needs them. In JavaScript, closures can prolong retention when persistent callbacks or event listeners capture values from an outer scope. Long-running applications may then experience progressively increasing memory use and degraded performance. section 3.1 section 3.4
Reason about a suspected leak by checking:
- Which closure, callback, or event listener still references the retained value? section 3.1
- Whether the listener remains active for the application's lifetime or beyond the component's useful lifetime. section 3.1
- How garbage collection determines whether the captured value is still reachable. section 3.1
A closure retained by an event listener
createHandler creates a large array and returns a click handler. Registering that handler with document.addEventListener can keep the closure and its captured largeObject alive while the listener remains registered. A safer lifecycle stores the handler, clears the large data when appropriate, and calls removeEventListener with the same handler reference.








