Skip to content
jshacks.dev
Debugging

Find detached DOM nodes in a heap snapshot

Locate DOM elements kept alive after removal by taking a heap snapshot in Chrome DevTools and filtering the class list for Detached.

1 min read
Summarize in AI

A detached node is an element removed from the page that JavaScript still references, so it cannot be freed. This code leaks one:

leak.jsJavaScript
const cache = [];

function openPanel() {
  const el = document.createElement("div");
  document.body.append(el);
  cache.push(el);
  el.remove();
}

Each call leaves a div in cache that is no longer in the document. To find them, open the Memory panel, choose Heap snapshot and take a snapshot after calling openPanel() a few times. In the filter box, type Detached:

Text
Detached HTMLDivElement

Expand the entry and its Retainers pane lists what holds the node: here, an index of the cache array. That path is the fix, since you must drop that reference. See Fix memory problems for the workflow.

Event listeners, timers and closures are the usual holders. Detached nodes keep their children and any data attached to them, so one small leak can hold a large subtree.

Take the snapshot after forcing garbage collection with the trash can button, so you see only nodes that really cannot be freed.

Also inBrowser
Share

More snippets

Debugging

Group related logs with console.group

Indent related console output under a label with console.group and console.groupEnd, so interleaved logs from one request stay readable in Node.js 24.

1 min read
APIs

Fetch JSON and handle HTTP errors

Fetch JSON in the browser or Node.js 24 and throw on 404 or 500 yourself, because fetch only rejects on network failure and does not check the status.

1 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.