Detecting Memory Leaks with DevTools Memory Profiler
You'll learn how to use Chrome DevTools' memory profiling tools to find and diagnose memory leaks in your JavaScript applications.
Why Visual Tools Matter
You've learned what memory leaks are and their common patterns—detached DOM nodes, forgotten event listeners, closures holding references, and unchecked timers. Now it's time to see them in action. Chrome DevTools provides powerful memory profiling tools that make invisible memory problems visible.
Heap Snapshots: Memory Freeze Frames
A heap snapshot captures everything in memory at a specific moment—like a photograph of your application's memory state. To use it:
- Open DevTools → Memory tab
- Select "Heap snapshot"
- Click "Take snapshot"
The snapshot shows all objects, their sizes, and what's retaining them. You can compare multiple snapshots to see what's growing between interactions. If you click a button 10 times and memory keeps increasing, compare snapshots before and after—the difference reveals what's leaking.
Allocation Timeline: Real-Time Memory Tracking
The allocation timeline (also called "Allocation instrumentation on timeline") records memory allocations as they happen—like a video instead of a photo. It shows blue bars when objects are allocated and gray bars when they're freed. Objects that stay blue weren't garbage collected, indicating potential leaks.
This is perfect for finding leaks during repeated actions: start recording, perform an action multiple times, stop recording. Objects that accumulate across iterations are your suspects.
The Retained Size View
DevTools shows both "shallow size" (the object itself) and "retained size" (everything it keeps alive). Focus on retained size—it reveals the true memory impact of leaked references.
Key Takeaway: Use heap snapshots to compare memory states before and after interactions, and allocation timelines to watch real-time memory behavior during repeated operations—both help pinpoint what's leaking and why.