Memory & front-end leaks
Understand the idea
Closing a panel removes its HTML, but memory keeps growing each time it opens. A long-lived listener still refers to its state.
Follow the reachable objects
Removing a panel from the screen does not prove its data is gone. A listener attached to window might still reference a that keeps the panel reachable. Thinking in terms of paths explains why an invisible screen can still occupy memory.
What garbage collection can reclaim
Creating an object allocates memory; losing your local does not necessarily make the object unreachable. Another object, a , a timer, or an event listener may still hold a reference. Cycles alone are not a leak in modern tracing collectors if the whole cycle becomes unreachable.
Garbage collection automatically reclaims memory that the program can no longer reach. You do not manually free every object in ordinary JavaScript. A cycle is not automatically a leak: two unreachable objects pointing to each other can still be collected. The question is whether some live root can still reach them.
| Retention source | Why data stays reachable | Cleanup decision |
|---|---|---|
| Window/document listener | A long-lived target keeps its handler | Remove the handler or abort its signal |
| Interval | Repeated future work keeps its callback | Clear the interval when its owner ends |
| Unbounded cache | Entries keep references indefinitely | Choose limits, eviction, or an appropriate weak collection |
| Detached DOM node | Another object still points to it | Remove obsolete references and handlers |
| Closure | A retained function can retain outer bindings | Keep only the state and lifetime the task needs |
Listeners and timers can retain state
A leak is often an ownership mistake: something long-lived retains data belonging to a finished feature. A listener on window can keep a closed panel's state reachable. A forgotten interval can keep running with a captured . A cache can grow forever if it has no removal policy.
Give long-lived work an owner
Cleanup needs the right . Removing a listener with a newly created arrow function does not remove the original . Keep the original function reference, or use an AbortSignal for a group of supported listeners. Clear intervals and disconnect observers when their owner is disposed.
Pair setup with cleanup. The code that starts a listener, interval, or should expose a way to stop it. A subscription is a registration for future updates; stopping it means the source no longer needs to keep your handler. The example returns a dispose function so the owner can end its listener's lifetime.
Investigate growth over repeated use
Do not guess from one memory reading. Garbage collection is not immediate, and developer tools can retain inspected objects. Reproduce repeated open/close cycles, compare heap snapshots, and inspect retaining paths. Measure whether the live set keeps growing after cleanup opportunities rather than treating every allocation as a leak.
Measure a repeated interaction, such as opening and closing the same panel many times. Use heap snapshots to inspect objects retained in memory and their retaining paths. One high reading is not proof of a leak, and console inspection itself may keep values reachable. Look for continued growth tied to a lifetime that should have ended.
Mental model
FOLLOW THE VALUE
A long-lived root can retain feature data.
01 / 03Trace the transformation
Live example
Only the first event is logged. Calling dispose removes the listener; the second event has no matching .
Your console output appears here.
Guided challenge
Dispose the listener
Run the returned cleanup function before dispatching the second event.
Your console output appears here.
EXPECTED CONSOLE OUTPUT
received
Hints 0/2
Use all hints or make 3 failed runs (0/3).
Free sandbox
Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.
Your console output appears here.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 200 XP. You can always revisit it.