Identifying Detached DOM Nodes
You'll learn how to spot DOM elements that were removed from the page but are still stuck in memory because JavaScript is holding onto them.
What Are Detached DOM Nodes?
A detached DOM node is an element that no longer exists in the visible page structure (it's been removed from the document), but JavaScript still has a reference to it somewhere. The garbage collector can't clean it up because your code is keeping it "alive."
Think of it like moving out of an apartment but still paying rent because you left the keys in your desk drawer. The apartment (DOM node) isn't being used, but it's still costing you memory because something is pointing to it.
How They Appear in Memory Profiler
In Chrome DevTools, when you take a heap snapshot:
- Look for yellow nodes in the snapshot summary—these indicate detached DOM elements
- Search for "Detached" in the constructor filter to see all detached nodes at once
- Check the retainer path to see what JavaScript variable is holding the reference
Common Causes
let button = document.querySelector('.my-button');
// User clicks something, button gets removed from DOM
button.parentElement.removeChild(button);
// But 'button' variable still exists!
// The element is now detached but not collected
The DOM node can't be garbage collected as long as that button variable exists, even though it's no longer visible or useful.
Finding and Fixing Them
In the snapshot, expand a detached node's retainer path. It will show you the chain leading back to a GC root—often a closure, an event listener, or a global variable. Nullify those references when you're done with removed elements.
Key Takeaway: Detached DOM nodes are removed from the page but still referenced by your JavaScript; use DevTools snapshots to find them and trace which variables are keeping them alive.