Skip to the page

Observers, workers and the platform · senior

Memory leaks in the browser

A leak is memory still reachable that you no longer need: listeners and timers never removed, detached DOM nodes held by JavaScript, growing caches and closures; find them with heap snapshots and fix them with cleanup.

The notes

What you must know

  • Common causes: setInterval not cleared, listeners on window not removed, detached nodes kept in arrays or closures, unbounded Maps used as caches, observers not disconnected.
  • WeakMap / WeakRef keys don't keep objects alive; good for metadata about DOM nodes.
  • DevTools Memory: take heap snapshots before and after an action, compare, search 'Detached'; the Performance monitor shows JS heap growth.
  • In SPAs, every effect that subscribes must return a cleanup.

The red pen

Common traps: mistake → fix

  • Mistake: "garbage collection means JS can't leak"

    Fix: GC frees only unreachable memory; your references keep things reachable.

Easy to hard

Questions interviewers ask, easy to hard

  1. What causes memory leaks in a single-page app?
  2. How would you find one?
  3. When would you use a WeakMap?

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview