How to Take and Compare Heap Snapshots in Microsoft Edge DevTools to Diagnose JavaScript Memory Leaks

As single-page applications (SPAs) execute prolonged client sessions, memory leaks frequently degrade tab responsiveness, increase garbage collection pauses, and eventually cause the browser tab to crash with an “Out of Memory” error. Memory leaks typically stem from forgotten event listeners, detached DOM trees held in global arrays, or lingering closure scopes. The Memory panel in Microsoft Edge DevTools allows developers to take point-in-time heap snapshots, inspect object retention trees, and compare multiple snapshots to isolate objects that fail to be garbage-collected.

Understanding the V8 Heap and Garbage Collection

In Microsoft Edge (powered by the Chromium V8 engine), JavaScript objects, arrays, and closures reside in the V8 heap. Garbage collection cleans up objects that are no longer reachable from “GC roots” (such as the global window object, active stack frames, or DOM wrappers). If a reference to an element remains stored in a JavaScript variable after that element is removed from the visible DOM, it becomes a “detached DOM tree”, consuming memory indefinitely.

Step-by-Step Practical Implementation

Step 1: Open Edge DevTools Memory Panel

  1. Open Microsoft Edge and load the application you wish to profile.
  2. Press F12 to open DevTools.
  3. Click the Memory tab along the top toolbar. (If hidden, click >>).
  4. Select the Heap snapshot radio button.

Step 2: Take Baseline Snapshot 1

  1. Click the Collect garbage button (trash can icon) to clean up transient objects.
  2. Click the blue Take snapshot button.
  3. DevTools records all heap allocations and lists Snapshot 1 in the left sidebar with its total byte size.

Step 3: Trigger the Suspected Leaking Workflow

  1. In your web application, perform the action suspected of leaking memory (e.g. open a modal dialog, filter a table, or navigate to a sub-view).
  2. Perform the reverse action (close the modal, reset filters, or navigate back).
  3. Click the Collect garbage button again.
  4. Click Take snapshot at the top of the Memory panel to record Snapshot 2.

Step 4: Compare Snapshot 2 Against Snapshot 1

  1. With Snapshot 2 selected, change the perspective dropdown at the top from Summary to Comparison.
  2. In the Select baseline snapshot dropdown, select Snapshot 1.
  3. The table sorts constructors by:
    • # Alloc: New instances created.
    • # Freed: Instances successfully reclaimed.
    • # Delta: Net instances remaining in memory (highlighting leaks).
    • Size Delta: Net memory change in bytes.

Step 5: Inspecting Retainers and Detached DOM Trees

  1. Filter the constructor list by typing Detached into the class filter input.
  2. Expand Detached HTMLDivElement to view lingering DOM nodes.
  3. Click any leaked instance and inspect the lower Retainers pane.
  4. The Retainers tree traces the exact chain of variable references connecting the detached node to the global root.

Heap Snapshot View Perspectives Reference

Perspective View Primary Display Key Columns Diagnostic Focus
Summary Constructors grouped by type Shallow Size, Retained Size, Distance Identifying largest memory consumers in a single snapshot
Comparison Net differences between two snapshots # Delta, Size Delta, # Alloc, # Freed Proving memory leaks across interactive user workflows
Containment V8 engine structure hierarchy Application DOM, V8 Heap, Native contexts Advanced low-level engine closure inspection
Statistics Pie chart of memory categories Code, Strings, JS Arrays, System Objects High-level memory allocation breakdown

Best Practices and Diagnostic Rules

  • Shallow vs Retained Size: Shallow Size is the memory held by the object itself (typically 32–64 bytes for object headers). Retained Size is the total memory that would be freed if this object was deleted and its reference tree dropped. Always sort by Retained Size.
  • Three-Snapshot Technique: Take Snapshot 1 (baseline), perform the action and reverse it 10 times, take Snapshot 2, repeat 10 more times, take Snapshot 3. Comparing Snapshot 3 against Snapshot 2 proves whether leaks continuously climb or stabilize.
  • Disable Edge Extensions: Browser extensions inject their own content scripts into the page heap. Always profile inside an InPrivate window with all extensions disabled.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.