Taking a Heap Snapshot
You'll learn how to capture a frozen picture of everything your JavaScript program has in memory right now.
What Is a Heap Snapshot?
A heap snapshot is like taking a photograph of your program's memory at a specific moment. It records every single object, variable, closure, and DOM element that exists in memory when you click the snapshot button. Unlike monitoring tools that show trends over time, a snapshot gives you a complete inventory of what's allocated right now.
Think of it like pausing a video game and seeing every character, item, and background element that's currently loaded—even the ones you can't see on screen.
How to Capture a Snapshot
Once you've opened the Memory Profiler in DevTools (which you learned in the previous lesson), you'll see a "Heap snapshot" option. Here's the process:
- Select "Heap snapshot" from the profiler types
- Click "Take snapshot" — the page freezes briefly while DevTools records everything
- Wait for processing — larger applications may take a few seconds
- View the results — you'll see a detailed breakdown of all objects
The snapshot shows constructors (like Array, Object, HTMLDivElement), how many instances of each exist, and their total memory size.
When to Take Snapshots
Take snapshots when you want to investigate potential leaks. A common technique is comparing two snapshots: take one, perform an action (like opening/closing a modal), then take another. If memory increased significantly, you can compare the snapshots to see what objects weren't cleaned up.
Key Takeaway: A heap snapshot captures every object in memory at a specific moment, giving you a complete picture to investigate what's currently allocated and hunt for memory leaks.