Skip to content
engineering workbench.by StudyPort Academy
Learning path
Advanced20 minLESSON 29 / 34

Memory & front-end leaks

01

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.

What keeps data reachable?
Retention sourceWhy data stays reachableCleanup decision
Window/document listenerA long-lived target keeps its handlerRemove the handler or abort its signal
IntervalRepeated future work keeps its callbackClear the interval when its owner ends
Unbounded cacheEntries keep references indefinitelyChoose limits, eviction, or an appropriate weak collection
Detached DOM nodeAnother object still points to itRemove obsolete references and handlers
ClosureA retained function can retain outer bindingsKeep 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.

02

Mental model

FOLLOW THE VALUE

window listener
reachable callback
captured state

A long-lived root can retain feature data.

01 / 03Trace the transformation

03

Live example

Only the first event is logged. Calling dispose removes the listener; the second event has no matching .

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022
04

Guided challenge

Dispose the listener

Run the returned cleanup function before dispatching the second event.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022

EXPECTED CONSOLE OUTPUT

received

Hints 0/2

    Use all hints or make 3 failed runs (0/3).

    05

    Free sandbox

    Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.

    Opening editor…
    LIVE PREVIEW
    Console 0

    Your console output appears here.

    Saved on this deviceJavaScript · ES2022
    06

    Quiz

    A quick check for understanding. Retry as often as you like; your learning path stays open.

    01Does a circular reference always cause a leak?
    02What does the returned dispose function do here?

    Ready to call this one understood?

    Mark this lesson complete to earn 200 XP. You can always revisit it.

    Sign in to save progress
    Next lesson