Finding Memory That Stays After Each Action in Chrome DevTools
A JavaScript memory leak where memory grows a little with each search or page switch can be narrowed down to the action that causes it, using Allocations on timeline in the Memory panel of Chrome DevTools. Objects created during the recording appear as bars on a chart. Objects still alive at the end are shown in blue, and objects cleaned up along the way are shown in gray. Selecting a single blue bar lists the objects that were created at that time and are still alive. Here I tested a small page that accumulates results on every search. I found the variable causing the leak, then checked that the bars turned gray after I fixed the code.
The test environment was Ubuntu 24.04 and Chrome for Testing 158.0.8127.36 (the one bundled with Playwright). I docked DevTools at the bottom of the window (Dock to bottom). The DevTools UI was in English, so the menus and buttons are written in English here. I served the page with a local HTTP server (http://localhost:8080/). I did not test pages built with libraries such as React, memory inside Web Workers, or Chrome on macOS and Windows.
- 1. A page that accumulates results on every search
- 2. Recording with Allocations on timeline
- 3. Five tall blue bars, one per action
- 4. Selecting one bar to count the objects that survived that search
- 5. Following the variable that holds the search results in Retainers
- 6. Confirming the function that created Item with a stack trace
- 7. In the fixed page, the earlier bars turn gray
A page that accumulates results on every search
This page has a button labeled "Search for 'scratching post'" (the label is in Japanese). When you press it, the page creates 20,000 products (Item) and collects the 4,001 products priced at 1,000 yen or less into search results (SearchResult). The app.js file is as follows.
class Item {
constructor(id) {
this.id = id;
this.name = `爪とぎ ${id} 号`;
this.price = 500 + (id % 30) * 100;
}
}
class SearchResult {
constructor(query, items) {
this.query = query;
this.items = items;
this.time = Date.now();
}
}
const history = [];
function search(query) {
const all = [];
for (let i = 1; i <= 20000; i++) all.push(new Item(i));
const hits = all.filter((item) => item.price <= 1000);
history.push(new SearchResult(query, hits));
return hits.length;
}
document.getElementById('search').addEventListener('click', () => {
const n = search('爪とぎ');
document.getElementById('result').textContent = `${n} 件見つかりました`;
});
Each search result is added to an array called history. Nothing ever empties this array. The screen shows a message meaning "4001 items found" each time, so nothing on screen suggests that anything is accumulating.
Recording with Allocations on timeline
The recording steps are as follows. They follow the same flow as the official documentation section on finding memory leaks with the Allocation Timeline.
- Open the Memory panel in DevTools and choose Allocations on timeline under Select profiling type
- Press Start at the bottom
- Repeat the action you suspect is leaking. Here I pressed the search button five times, about 2 seconds apart
- Stop recording with the red circle button at the top left (Stop recording heap profile)

I left about 2 seconds between actions so that each action's bar would be clearly separated on the timeline.
Five tall blue bars, one per action
When I stopped recording, "Snapshot 1 (3.4 MB)" appeared under Allocation timelines in the list on the left. On the right, the timeline is at the top and a table is below it. The timeline showed five tall blue bars about 2 seconds apart, with a few very short bars in between. The number of tall bars matched the number of times I pressed the search button.

According to the official documentation section on reading a recording, a bar's height is the size of the objects created at that time, and its color shows whether the object is still alive at the end of the recording. Blue means the object still exists at the end of the recording. Gray means the object was created during the recording and then cleaned up by garbage collection. All five bars were blue, so the objects created by every search were still alive when I stopped recording.
Before I selected a range on the timeline, the table below (Summary) showed the following rows for SearchResult and Item.
| Constructor | Count | Retained size |
|---|---|---|
| SearchResult | 5 | 1,249 kB |
| Item | 20,005 | 1,160 kB |
There were 5 SearchResult objects, matching the number of searches, and 20,005 Item objects, which is 4,001 for each of the five searches.
Selecting one bar to count the objects that survived that search
If I dragged across the timeline so that the selection covered one bar, the table only showed objects created and still alive in that range. When I selected the third bar (5.4 to 7.5 s), the count above the table was 8,168 objects (283 kB), with 1 SearchResult and 4,001 Item objects.

I selected each of the other bars in turn and checked the SearchResult and Item rows.
| Selected range | Count shown above the table | SearchResult | Item |
|---|---|---|---|
| 1 to 3.2 s (1st bar) | 9,129 objects (385 kB) | 1 | 4,001 |
| 3.2 to 5.4 s (2nd bar) | 8,072 objects (252 kB) | 1 | 4,001 |
| 5.4 to 7.5 s (3rd bar) | 8,168 objects (283 kB) | 1 | 4,001 |
| 7.5 to 9.6 s (4th bar) | 8,035 objects (257 kB) | 1 | 4,001 |
| 9.6 to 11.8 s (5th bar) | 8,082 objects (252 kB) | 1 | 4,001 |
| 11.8 to 29.5 s (part without bars) | 61 objects (82.1 kB) | no row | no row |
Every bar retained the full result of one search. If each action leaves the same amount behind, you can guess that the action drops no reference to its data somewhere along the way.
Following the variable that holds the search results in Retainers
I selected the SearchResult row in the table and opened it with the right arrow key. One item, SearchResult @128593, appeared. The app.js:10 shown on the right is the constructor line of SearchResult. When I selected it, the Retainers pane below listed what references this search result.
![With SearchResult @128593 selected in the table above, the Retainers pane below starts with [2] in Array, followed by history in system / Context / scope](https://neko-note.org/wp-content/uploads/2026/11/chrome-devtools-allocation-timeline-retainers.png)
The first two rows were as follows. I omitted the numbers after the @.
[2] in Array
history in system / Context / scope
The object is held as [2] in an Array, meaning the third element, and that array is referenced by the variable history. I had selected the third bar, which matches the fact that the result of the third search is stored at history[2]. This shows that the cause is that results are only ever added to history, never removed or taken out.
Confirming the function that created Item with a stack trace
If I turn on Allocation stack traces (more overhead) under Allocations on timeline in Select profiling type and record again, the call history (stack trace) of the function that created each object is also recorded. I recorded the same actions with this setting, selected the third bar, and opened the Item row. I selected one of the Item objects and switched the lower pane from Retainers to the Allocation stack tab. It showed the following.

Line 19 of app.js is function search(query) {. Line 27 is where the click listener for the button is registered. The flow is clear: the click handler calls search(), and that is where the Item objects are created.
When I selected one of the SearchResult objects in the same recording, the Allocation stack tab showed "Stack wasn’t recorded for this object because it had been allocated before this profile recording started", and no stack was shown. The object was created by a search after recording had started, but it still showed this message.
In the fixed page, the earlier bars turn gray
I changed the code to stop accumulating results in history and keep only the most recent search result.
-const history = [];
+let lastResult = null;
function search(query) {
const all = [];
for (let i = 1; i <= 20000; i++) all.push(new Item(i));
const hits = all.filter((item) => item.price <= 1000);
- history.push(new SearchResult(query, hits));
+ lastResult = new SearchResult(query, hits);
return hits.length;
}
I opened the fixed page in a new Chrome profile, searched five times in the same way, and recorded. Most of the bars for the first four searches turned gray (some kept a short blue base), and only the fifth bar stayed fully blue.

When I selected each bar and checked the table, the ranges for bars 1 to 4 had no SearchResult or Item rows. Only the fifth bar (9.6 to 11.8 s) showed one SearchResult and 4,001 Item objects. Each time a new result was stored in lastResult, the previous result lost all references and was cleaned up, so only the last search's result remained. The heap size at the time recording stopped also dropped, from 3.4 MB (75,387 objects) before the fix to 2.4 MB (43,286 objects) after it. This is shown by Snapshot 1 in the list on the left and by the count above the table.
Even after the fix, the bar for the last action remains blue. Keeping the most recent result is the intended behavior for this page. Do not assume a leak just because a blue bar exists. Repeat the action and check whether blue bars from earlier actions keep piling up. If they do, something is accumulating.





