← Lessons

quiz vs the machine

Platinum1850

Frontend

The Memory Leak Detection

Find and fix memory that a long lived page holds onto forever, before it slows the tab to a crawl.

6 min read · advanced · beat Platinum to climb

When memory never comes back

A memory leak happens when a page keeps references to objects it no longer needs, so the garbage collector cannot reclaim them. In a long lived single page app this memory grows over time, eventually making the tab sluggish or crashing it. Detecting leaks means watching memory across repeated actions.

  • Memory should return to a baseline after a repeated action.
  • A steady upward trend across cycles signals a leak.
  • Leaks hurt most in apps the user keeps open for hours.

Common sources

A few patterns cause most front end leaks.

  • Forgotten listeners keep handlers and their scope alive.
  • Detached nodes are removed from the page but still referenced by script.
  • Growing caches store entries that are never evicted.
  • Timers that are never cleared hold their closures forever.

A detection workflow

  • Record a baseline, perform an action several times, and compare heap snapshots.
  • Look for objects whose count climbs with each cycle.
  • Trace the retaining path to find what still references them.

Fixing leaks

  • Remove listeners when a component unmounts.
  • Clear intervals and timeouts you started.
  • Bound caches with a size limit or weak references.

Key idea

A memory leak is retained objects the collector cannot free, so detect it by watching the heap grow across cycles and fix it by releasing listeners, timers, and unbounded caches.

Check yourself

Answer to earn rating on the learn ladder.

1. What is a memory leak in a front end app?

2. Which pattern commonly causes a leak?

3. How do you confirm a leak with heap snapshots?