日本語

Finding Detached DOM Elements That Stay in Memory with Chrome DevTools

​​Chrome DevTools​JavaScript

Elements that have been removed from the screen but are still held in memory because JavaScript references them, such as a closed notification or a list that was replaced, can be listed with Detached elements in the Memory panel of Chrome DevTools. The HTML of each remaining element and the number of nodes inside it are shown as they are. When you suspect a memory leak, where memory keeps growing while the page stays in use, this is a good first step for checking whether DOM elements are still being kept alive.

Note

The test environment was Ubuntu 24.04 and Chrome for Testing 153.0.8010.12 (the one bundled with Playwright). I docked DevTools to the bottom of the window (Dock to bottom). The DevTools UI was in English, so the menu and button names below are written in English. The page was served from an HTTP server I started locally (http://localhost:8080/). I did not test pages that include iframes, pages built with libraries such as React, or Chrome on macOS and Windows.

Preparing a page where elements remain

The page I tested has two buttons. Pressing "Show notice" shows a notice with a "Close" button. Pressing "Reload products" rebuilds a list of 50 products. Clicking a row in the list adds the selected class to that row, which changes its color. app.js is as follows.

js
// Notices: keep the shown ones in an array so they can be listed later
const notices = [];
let n = 0;

function showNotice() {
  n++;
  const message = `お知らせ ${n}:在庫が入りました`;
  const toast = document.createElement('div');
  toast.className = 'toast';
  toast.textContent = message;
  const close = document.createElement('button');
  close.textContent = '閉じる';
  close.addEventListener('click', () => toast.remove());
  toast.appendChild(close);
  notices.push(toast);
  document.getElementById('toasts').appendChild(toast);
}

// Product list: remember the selected row, and clear its color the next time a row is selected
let selected = null;

function renderList() {
  const list = document.createElement('ul');
  for (let i = 1; i <= 50; i++) {
    const li = document.createElement('li');
    li.textContent = `商品 ${i}`;
    list.appendChild(li);
  }
  list.addEventListener('click', (event) => {
    const li = event.target.closest('li');
    if (!li) return;
    selected?.classList.remove('selected');
    selected = li;
    li.classList.add('selected');
  });
  document.getElementById('results').replaceChildren(list);
}

document.getElementById('notify').addEventListener('click', showNotice);
document.getElementById('reload').addEventListener('click', renderList);
renderList();

This page has two places where elements are kept alive. First, the notice elements themselves are stored in the notices array. Even after the Close button removes them from the screen, the array still holds them. Second, the selected row is kept in the variable selected while the list is rebuilt.

After opening the page, I did the following in order.

  1. Pressed "Show notice" three times, then closed all three notices with the Close button
  2. Clicked "Product 3" in the list
  3. Pressed "Reload products" twice

After these steps, no notices were visible, and only the 50 items of the rebuilt list were shown. Nothing on the screen indicated that anything was still held.

Getting Detached elements from the Memory panel

The official documentation on finding memory problems describes Detached elements as showing DOM elements that are detached from the DOM but retained because JavaScript code references them. In the test environment, I did the following.

  1. Open the Memory panel in DevTools. You can also open it by choosing Show Memory from the command menu (Ctrl+Shift+P).
  2. In the Select profiling type list, select Detached elements at the bottom.
  3. Press the Take snapshot button below.
The Select profiling type list in the Memory panel. Heap snapshot, Allocations on timeline, Allocation sampling, and Detached elements are listed, with Detached elements selected. The description reads "Detached elements show objects retained by a JS reference."
Detached elements selected in Select profiling type

In the test environment, the button below still read Take snapshot after selecting Detached elements. The recording button in the row above the panel, however, was labeled Get detached elements. Pressing Take snapshot added "Detached elements 1" to the list on the left and showed a table on the right.

A table with two columns, Detached nodes and Node count. The row for <ul> has a Node count of 101, and there are three rows for <div class="toast">, each with a count of 4. Opening the first toast row shows the notice text, meaning "Notice 1: Stock has come in", and a Close button. (The labels are in Japanese in the image.)
The captured list, with the first notice row expanded

The table had one row for <ul> and three rows for <div class="toast">. These are the three closed notices and one of the lists from before it was rebuilt.

Reading Node count and the contents of an expanded row

The Node count on the right is the number of nodes (elements and text parts) in the element itself plus everything inside it. In the DevTools source code, the count is calculated by adding the number of child nodes to the element's own 1. For the notice, the count of 4 consists of the <div>, the notice message text, the <button>, and the Close text, which makes four. For the list, the count of 101 consists of one <ul>, 50 <li> elements, and 50 text nodes with the product names, which makes 101. Expanding a row shows the text inside, so you can see which notice is still being held.

Select the <ul> row and press the → key to list all of the <li> elements inside it.

The expanded <ul> row. After the rows for Product 1 and Product 2, there is <li class="selected">Product 3</li>, followed by more <li> elements up to Product 10. The Node count is 101. (The labels are in Japanese in the image.)
The list from before the rebuild, expanded

The third row is the <li class="selected"> element for Product 3. This is the row I selected before the list was rebuilt. From this, I could infer that the variable selected holds the <li> for Product 3, which keeps its parent <ul> and the other 49 rows in memory as well. Because an <li> holds a reference to its parent <ul> (through parentNode), keeping just one row alive prevents the whole list from being freed. This mechanism is also explained in the DOM leak example on the official heap snapshot page.

Point

The Detached elements table has only two columns, Detached nodes and Node count. It does not show which variable or code references an element. When you cannot make a guess from class names or text the way you could with the page in this article, take a heap snapshot and follow the Retainers section for the element (see the official Retainers section).

Elements that are not referenced do not appear in the list

I pressed "Reload products" twice, so there are two lists that were removed from the screen. The first was the list where Product 3 was selected, and the second was the list with nothing selected. Only the first one appeared in the table.

To check whether the second list was still in memory, I used the Chrome DevTools Protocol from outside the page's own code, before pressing the button a second time, to watch that list with a WeakRef (a reference that does not prevent garbage collection even though it points to the object). Before I pressed Take snapshot, the second list was still in memory. After I pressed it, the list was gone from memory.

DevTools' Detached elements list is retrieved by calling DOM.getDetachedDomNodes in the Chrome DevTools Protocol (DevTools source code). So I created one detached element with no references, watched it with a WeakRef, and compared two cases: calling this method directly without going through DevTools, and simply waiting about 0.7 seconds without calling it. (I tried each case once.) Waiting alone left the element in memory. After the method was called, the element was gone, and it was not included in the returned list.

In the test environment, the result was the same without first pressing the Collect garbage button in the Memory panel: the list only contained elements that were still held by JavaScript references.

Fixing the page and capturing again

I changed the notices to store the message text instead of the element, and I changed the code so that selected is cleared when the list is rebuilt.

diff
-  notices.push(toast);
+  notices.push(message);
diff
 function renderList() {
+  selected = null;
   const list = document.createElement('ul');

I started Chrome again with a new profile, repeated the same steps on the fixed page, and captured Detached elements. The table had no rows.

A table showing only the Detached nodes and Node count headers, with no rows
The list captured from the fixed page

This table confirms that neither the closed notices nor the list from before the rebuild remain as detached elements.

​Posted by 86