1// ❌ Memory Leak: Interval keeps firing forever in the background!
2useEffect(()=>{
3const id =setInterval(pollServer,3000);
4},[]);// Missing return () => clearInterval(id);
Leak 3: Detached DOM Nodes Held by Closures
A Detached DOM Node occurs when a DOM element has been removed from the page's visible DOM tree, but a lingering JavaScript variable, closure, or event callback still holds a reference to it in memory, preventing the browser's Garbage Collector from freeing its memory.
3. Detecting Leaks with Chrome DevTools Memory Profiler
To diagnose real memory leaks:
1
Open Chrome DevTools -> Memory tab.
2
Select Allocation instrumentation on timeline or Heap snapshot.
3
Perform the user action (e.g. open a modal, then close it).
4
Click the garbage can icon to Collect Garbage (GC) manually.
5
Take a Heap Snapshot.
6
In the Class Filter, search for Detached.
If you see Detached HTMLDivElement or Detached FiberNode, inspect the Retainers Tree at the bottom of the panel to see the exact closure or reference preventing garbage collection!
Interactive Quiz• Knowledge Check
Practice Quiz
Question 1
Why are memory leaks significantly more dangerous in Single-Page Applications (SPAs) than in traditional multi-page websites?
Question 2
What is a "Detached DOM Node"?
Question 3
How do you prevent timer memory leaks in useEffect?
Question 4
Which panel in Chrome DevTools allows engineers to take Heap Snapshots and inspect detached DOM elements?
Question 5
What does clicking the "Collect Garbage" (trash can) icon in DevTools Memory panel do?
Next Lesson
Profiling Component Render Durations with React DevTools
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.