Skip to content
Tutorials Center
My learning

Topics

Lesson 26 of 36 ยท Application and Storage

The Memory Panel and Heap Snapshots

Take heap snapshots, compare them, understand shallow versus retained size, detached DOM nodes and allocation profiling, and learn the three-snapshot technique.

JavaScript frees memory automatically with a garbage collector (GC). It removes objects that nothing can reach anymore. A memory leak in JavaScript is therefore not forgotten free() calls; it is an object you no longer need that something still references, so the collector must keep it. Over minutes of use, such pages grow slower and finally crash a tab.

The Memory panel lets you look inside the heap. This lesson explains the three profiling modes, how to read a snapshot, and the repeatable technique for finding what is retained.

The three profiling types

Open the Memory panel and choose one option, then click Take snapshot or Start:

  1. Heap snapshot: a frozen picture of every object in the JavaScript heap, including DOM nodes. Best for finding leaks and for seeing "what holds this object?". It pauses the page while it is taken and can take seconds on large heaps.
  2. Allocation instrumentation on timeline: records allocations over time as blue bars; bars that stay blue are objects that were never collected. Good for finding which code allocates memory that sticks.
  3. Allocation sampling: a lightweight sampling profiler with low overhead showing which functions allocate the most, suitable for long runs.

There is also the Performance panel's Memory checkbox, which draws a graph of JS heap, documents, DOM nodes and listeners during a recording: a quick way to notice a rising line before you dig deeper, and the Performance monitor (Command menu > "Show Performance monitor") for a live graph.

Reading a heap snapshot

After taking a snapshot, a table lists constructors (object types): Object, Array, (string), HTMLDivElement, your own classes, and so on. The view dropdown switches the perspective:

  • Summary: objects grouped by constructor, with counts and sizes. Use the search box to find your class.
  • Comparison: shows the difference to an earlier snapshot, with columns for objects added (# New), deleted (# Deleted), and the size change. This is the leak finder.
  • Containment: the raw object graph from the roots (the window, GC roots).
  • Statistics: a pie chart of memory by kind (code, strings, JS arrays, system).

Two size columns confuse everyone at first:

  • Shallow size: the memory held by the object itself.
  • Retained size: the memory that would be freed if this object were deleted, including everything only reachable through it. This is the number to sort by when searching for the big offenders.

Select an object and look at the Retainers pane at the bottom. It shows the chain of references that keeps the object alive, for example window > cache > Map > entry > yourObject. The leak is almost always visible in that chain. Objects tinted yellow are referenced from JavaScript directly; red ones are detached DOM nodes: elements removed from the page but still referenced, a classic leak. A quick filter in Summary view, "Detached", lists them.

Retained size, as a computation

Retained size follows from the reference graph. The stand-in below models a tiny heap, finds everything reachable from the roots, and computes the retained size of each object by deleting it and measuring what would become unreachable, which is exactly the definition.

JS Stand-in: compute retained size on a reference graph

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice that blob is large (400) but entryA alone retains only 100, because entryB also references the blob. Only when both entries go does the blob become garbage. Retained sizes depend on sharing, which is why DevTools computes them for you.

The three-snapshot technique

To find a leak that happens when the user repeats an action (open and close a dialog, navigate between views):

  1. Load the page and perform the action once so that caches and lazy initialization are done. Click the trash can icon to force a GC.
  2. Take Snapshot 1.
  3. Repeat the action several times (say 10).
  4. Force GC again and take Snapshot 2.
  5. Repeat another 10 times, force GC, take Snapshot 3.
  6. In Snapshot 3 choose Comparison against Snapshot 2. Look for constructors whose # New is about 10 and # Deleted is much smaller. Objects that grow by the same amount between two equal runs are leaking.
  7. Select a leaked object and read its Retainers chain to find the reference that must be released.

Using two comparison runs avoids false positives from warm-up allocations.

Typical leak causes

  • Event listeners added on window or document and never removed.
  • Timers (setInterval) that keep closures alive.
  • Detached DOM nodes kept in a variable or array.
  • Unbounded caches and global arrays (logs, history).
  • Closures capturing large objects.

A stand-in for the third approach, comparing two "snapshots" of object counts, helps you read a comparison table.

JS Stand-in: compare two snapshots by constructor

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Dialog and HTMLDivElement each grew by exactly the amount of work done between snapshots (10 dialogs and 100 nodes), so they are the leak suspects, not the many small fluctuations.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does retained size mean?

    Retained size includes everything reachable only through the object; shallow size is the object itself.
  2. 2. In JavaScript, a memory leak mostly means:

    The collector can only free unreachable objects; a forgotten reference keeps them alive.
  3. 3. Which snapshot view shows objects added since an earlier snapshot?

    Comparison lists # New, # Deleted and size deltas against a chosen snapshot.
  4. 4. Why force garbage collection (trash icon) before taking a snapshot?

    Uncollected garbage would show up as new objects and hide real leaks.

Summary

  • Memory panel modes: heap snapshot, allocation instrumentation on timeline, allocation sampling.
  • Summary, Comparison, Containment and Statistics views answer different questions; Comparison finds growth.
  • Sort by retained size and read the Retainers chain to see what keeps an object alive.
  • The three-snapshot technique with forced GC separates real leaks from warm-up noise.
  • Common leaks: forgotten listeners, timers, detached DOM nodes, unbounded caches.

Next: Running Your First Lighthouse Audit