Course contentsShow
← JavaScript
Lesson 1887 of 2,72339. Memory Management and GCFree lesson

Comparing Two Snapshots

Taking before-and-after snapshots to identify objects that weren't garbage collected as expected.

Comparing Two Snapshots

What you'll learn: How to use before-and-after heap snapshots to catch objects that should have been garbage collected but weren't.

Why Compare Snapshots?

A single snapshot shows your memory state at one moment, but it can't tell you what changed. By taking two snapshots—one before an action and one after—you can identify which objects were created and, more importantly, which ones weren't cleaned up when you expected them to be.

The Basic Workflow

  1. Take the first snapshot before performing an action (like opening a modal, loading data, or running a function)
  2. Perform the action that you suspect might leak memory
  3. Clean up (close the modal, clear data, etc.)
  4. Take the second snapshot
  5. Compare them in DevTools

What the Comparison Shows

When you compare snapshots, DevTools highlights:

  • New objects created between snapshots
  • Deleted objects that were garbage collected
  • Delta (Δ) - the size difference

The danger zone is when you see objects that should have been deleted but remain in memory with positive deltas.

Reading the Comparison View

Look for constructor names with unexpected positive counts. If you closed a component but still see 15 new event listeners or DOM nodes, you've found a leak. The comparison view filters out noise and shows you exactly what changed, making leaks much easier to spot than scanning a full snapshot.

Key Takeaway: Comparing before-and-after snapshots reveals objects that survived when they should have been garbage collected, making memory leaks visible through their delta counts.