Skip to the page

Performance regressions · senior

Memory leaks in long-lived tabs

A dashboard left open all day can grow until the tab crashes. You confirm the leak, find what keeps old objects alive with heap snapshots, and fix it with cleanup.

The notes

What you must know

  • Confirm: watch memory over time (the Performance monitor, Task Manager). If the heap keeps growing after garbage collection, it is a leak.
  • Quick relief: a soft reload while the tab is idle (keep filters in the URL), slower polling and a cap on stored history.
  • Diagnose with the three-snapshot method: take a baseline, repeat the action, compare. Filter for "Detached" nodes, and follow the retainers to the code that holds them. The allocation timeline also helps.
  • Common causes: listeners or setInterval not cleaned up in useEffect, a socket subscription added on every mount, chart instances never destroyed, a global Map used as a cache that only grows, closures holding old data.
  • Here: each tab switch mounts a chart that subscribes to the socket and adds a resize listener with no cleanup. Old charts stay reachable through the listener list.
  • Fix: return cleanup functions, use AbortController for listeners and fetches, call the chart destroy(), use a bounded (LRU) cache and keep only the last N data points.
  • Test faster by repeating the action in a loop (for example 200 tab switches with Playwright) and comparing heap sizes.
  • Prevent: a heap-growth test in CI for key flows (Playwright or a tool like memlab) and a lint rule or review checklist for effects without cleanup.

The red pen

Common traps: mistake → fix

  • Mistake: Garbage collection removes every unused object

    Fix: anything still reachable from a listener, timer or global stays.

  • Mistake: A leak needs a huge page

    Fix: small leaks per action add up over hours.

  • Mistake: Memory growth always means a leak

    Fix: check that it stays high after garbage collection.

Easy to hard

Questions interviewers ask, easy to hard

  1. A dashboard tab becomes slow and crashes after a day. How do you find the cause?
  2. How do you use the three-snapshot method?
  3. Name four causes of leaks in React apps.
  4. How can you test for leaks automatically?

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