Course contentsShow
← Web Development
Lesson 2116 of 2,98238. JavaScript Performance in the BrowserFree lesson

The Three-Snapshot Technique

Master the practical workflow of taking heap snapshots before, during, and after actions to isolate leaks.

The Three-Snapshot Technique

You'll learn a systematic workflow for isolating memory leaks by comparing heap snapshots taken at strategic moments.

What Is the Three-Snapshot Technique?

The Three-Snapshot Technique is a practical debugging method where you capture three heap snapshots—before, during, and after performing an action—then compare them to identify objects that should have been garbage collected but weren't.

Think of it like checking your kitchen for leaks: you note the floor's condition (snapshot 1), run the dishwasher (the action), check immediately after (snapshot 2), then check again later (snapshot 3). If water remains where it shouldn't, you've found your leak.

The Workflow

Snapshot 1 (Baseline): Take a heap snapshot before any suspicious action. This establishes your memory baseline.

Perform Action: Execute the operation you suspect is leaking—open a modal, load data, switch views, etc. Repeat it several times (5-10x) to amplify any leak signal.

Snapshot 2 (During): Take a snapshot immediately after the action. Memory use will naturally be higher.

Snapshot 3 (After Cleanup): Trigger any cleanup (close modal, navigate away), wait a few seconds for garbage collection, then take a final snapshot.

Finding the Leak

In DevTools Memory Profiler, compare Snapshot 3 to Snapshot 1. Objects that increased in count between these two snapshots—despite returning to the "clean" state—are your leak candidates. Look for detached DOM nodes, event listeners, or retained closures that should have been freed.

This technique isolates the leak from normal memory fluctuations by focusing on what persists after cleanup, not just what temporarily increases.

Key Takeaway: The Three-Snapshot Technique systematically isolates memory leaks by comparing before, during, and after states—retained objects in the final snapshot reveal what failed to be garbage collected.