Skip to article content
FrontendXLesson 17 / 20

Memory, Garbage Collection, and Leaks

Build an accurate mental model of what the browser does, from navigation and HTTP through parsing, rendering, storage, workers, security, and DevTools, so you can predict behavior and debug real pages.

advanced · about 45 minutes · 2 exercises

  • Predict
  • Debug

What you will be able to do

#
  • Use heap snapshots to find growth.
  • Identify common leak sources.
  • Use WeakMap and WeakRef for caches.

Overview

#

JavaScript memory is reclaimed by garbage collection when objects are unreachable.

Finding leaks

Take a heap snapshot, interact, take another, and compare. Look for objects that grow without bound. The allocation timeline shows where allocations happen over time.

Common leak sources

  • Detached DOM nodes held by a JavaScript reference after removal.
  • Event listeners on long-lived targets that capture large closures.
  • Timers and intervals that are never cleared.
  • Growing caches and arrays without eviction.
  • Global variables that hold data forever.

Tools

Use WeakMap and WeakRef for caches that should not keep objects alive. In DevTools, the Detached Elements view and the heap snapshot retainer path show what is keeping an object alive.

Measuring

Force garbage collection in the Memory panel before comparing snapshots so short-lived garbage does not confuse the diff.

Questions to carry forward

  • How do you find what retains a detached node?
  • Why can an event listener leak memory?
  • When is WeakRef appropriate?

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Debug

    This component leaks memory on every mount and unmount cycle. Identify the leak and fix it.

    starter.javascript
    useEffect(() => {
      const id = setInterval(() => poll(), 1000);
    }, []);

    Reveal the reference answer
    reference.javascript
    useEffect(() => {
      const id = setInterval(() => poll(), 1000);
      return () => clearInterval(id);
    }, []);

    Authored expected output: The interval is cleared on unmount, so poll is not retained.

    Without a cleanup function, the interval keeps running and retains its closure and any captured data after unmount.

  2. Exercise 2Predict

    Which structure is appropriate for a cache that must not keep its keys alive?

    Choose the best answer

Common misconceptions

#
  • Removing a node from the DOM frees it immediately if JavaScript still references it.
  • Garbage collection runs on a fixed schedule you can rely on.
  • A WeakMap key keeps the object alive.

Follow-up practice

#
  • browser-internals-debugging-capstone

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.