豆豆友情提示:这是一个非官方 GitHub 代理镜像,主要用于网络测试或访问加速。请勿在此进行登录、注册或处理任何敏感信息。进行这些操作请务必访问官方网站 github.com。 Raw 内容也通过此代理提供。
Skip to content

Commit d19a235

Browse files
devnookyshnggdependabot[bot]OrKoNmathiasbynens
authored
feat: add a skill for detecting memory leaks using take_memory_snapshot tool (#1162)
This PR add a new skill for detecting and removing memory leaks using take_memory_snapshot tool. Skill was based on authoritative material from own sources: * https://developer.chrome.com/docs/devtools/memory-problems * https://web.dev/articles/effectivemanagement as well as examples of memory leaks from public domain. It can be tested with https://github.com/devnook/mem-leak-example. Example prompt * A page running at localhost:3000 has memory issues when you click from home to contact tab and back. Can you fix it? The skill prefers using comparison tools like memlab instead of custom evaluate_script snippets to avoid directly reading of .heapsnpashot files to save context space and tokens. Fixes #1056 --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: Yusheng <113574546+yshngg@users.noreply.github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Alex Rudenko <OrKoN@users.noreply.github.com> Co-authored-by: Mathias Bynens <mathias@qiwi.be>
1 parent 9a47b65 commit d19a235

File tree

5 files changed

+222
-0
lines changed

5 files changed

+222
-0
lines changed

eslint.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export default defineConfig([
3737
'puppeteer.config.cjs',
3838
'eslint.config.mjs',
3939
'rollup.config.mjs',
40+
'skills/memory-leak-debugging/references/compare_snapshots.js',
4041
],
4142
},
4243
},
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
name: memory-leak-debugging
3+
description: Diagnoses and resolves memory leaks in JavaScript/Node.js applications. Use when a user reports high memory usage, OOM errors, or wants to analyze heapsnapshots or run memory leak detection tools like memlab.
4+
---
5+
6+
# Memory Leak Debugging
7+
8+
This skill provides expert guidance and workflows for finding, diagnosing, and fixing memory leaks in JavaScript and Node.js applications.
9+
10+
## Core Principles
11+
12+
- **Prefer `memlab`:** Do NOT attempt to read raw `.heapsnapshot` files directly, as they are extremely large and will consume too many tokens. Always recommend and use `memlab` to process snapshots and identify leak traces.
13+
- **Isolate the Leak:** Determine if the leak is in the browser (client-side) or Node.js (server-side).
14+
- **Common Culprits:** Look for detached DOM nodes, unhandled closures, global variables, event listeners not being removed, and caches growing unbounded. _Note: Detached DOM nodes are sometimes intentional caches; always ask the user before nulling them._
15+
16+
## Workflows
17+
18+
### 1. Capturing Snapshots
19+
20+
When investigating a frontend web application memory leak, utilize the `chrome-devtools-mcp` tools to interact with the application and take snapshots.
21+
22+
- Use tools like `click`, `navigate_page`, `fill`, etc., to manipulate the page into the desired state.
23+
- Revert the page back to the original state after interactions to see if memory is released.
24+
- Repeat the same user interactions 10 times to amplify the leak.
25+
- Use `take_memory_snapshot` to save `.heapsnapshot` files to disk at baseline, target (after actions), and final (after reverting actions) states.
26+
27+
### 2. Using Memlab to Find Leaks (Recommended)
28+
29+
Once you have generated `.heapsnapshot` files using `take_memory_snapshot`, use `memlab` to automatically find memory leaks.
30+
31+
- Read [references/memlab.md](references/memlab.md) for how to use `memlab` to analyze the generated heapsnapshots.
32+
- Do **not** read raw `.heapsnapshot` files using `read_file` or `cat`.
33+
34+
### 3. Identifying Common Leaks
35+
36+
When you have found a leak trace (e.g., via `memlab` output), you must identify the root cause in the code.
37+
38+
- Read [references/common-leaks.md](references/common-leaks.md) for examples of common memory leaks and how to fix them.
39+
40+
### 4. Fallback: Comparing Snapshots Manually
41+
42+
If `memlab` is not available, you MUST use the fallback script in the references directory to compare two `.heapsnapshot` files and identify the top growing objects and common leak types.
43+
44+
Run the script using Node.js:
45+
46+
```bash
47+
node compare_snapshots.js <baseline.heapsnapshot> <target.heapsnapshot>
48+
```
49+
50+
The script will analyze and output the top growing objects by size and highlight the 3 most common types of memory leaks (e.g., Detached DOM nodes, closures, Contexts) if they are present.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Common Memory Leaks
2+
3+
When analyzing a retainer trace from `memlab`, look for these common patterns in the codebase:
4+
5+
## 1. Uncleared Event Listeners
6+
7+
Event listeners attached to global objects (like `window` or `document`) or long-living objects prevent garbage collection of the objects referenced in their callbacks.
8+
9+
**Fix:** Always call `removeEventListener` when a component unmounts or the listener is no longer needed.
10+
11+
## 2. Detached DOM Nodes
12+
13+
A DOM node is removed from the document tree but is still referenced by a JavaScript variable. While detachedness is a good signal for a memory leak, it's not always a bug. For example, websites sometimes intentionally cache detached navigation trees.
14+
15+
**Fix:** Signal the detached nodes to the user first. **Ask the user first** before nulling the references or changing the code, as the detached nodes might be part of an intentional cache. If confirmed as a leak, ensure variables holding DOM references are set to `null` when the node is removed, or limit their scope.
16+
17+
## 3. Unintentional Global Variables
18+
19+
Variables declared without `var`, `let`, or `const` (in non-strict mode) or explicitly attached to `window` remain in memory forever.
20+
21+
**Fix:** Use strict mode, properly declare variables, and avoid global state.
22+
23+
## 4. Closures
24+
25+
Closures can unintentionally keep references to large objects in their outer scope.
26+
27+
**Fix:** Nullify large objects when they are no longer needed, or refactor the closure to not capture unnecessary variables.
28+
29+
## 5. Unbounded Caches or Arrays
30+
31+
Data structures used for caching (like objects, Arrays, or Maps) that grow without limits.
32+
33+
**Fix:** Implement caching limits, use LRU caches, or use `WeakMap`/`WeakSet` for data associated with object lifecycles.
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/**
2+
* @license
3+
* Copyright 2025 Google LLC
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
import * as fs from 'node:fs';
8+
9+
function parseSnapshot(filePath) {
10+
console.log(`Loading ${filePath}...`);
11+
const data = JSON.parse(fs.readFileSync(filePath, 'utf8'));
12+
const strings = data.strings;
13+
const nodes = data.nodes;
14+
const nodeFields = data.snapshot.meta.node_fields;
15+
const nodeFieldCount = nodeFields.length;
16+
17+
const typeOffset = nodeFields.indexOf('type');
18+
const nameOffset = nodeFields.indexOf('name');
19+
const sizeOffset = nodeFields.indexOf('self_size');
20+
21+
const nodeTypes = data.snapshot.meta.node_types[typeOffset];
22+
23+
const counts = {};
24+
const sizes = {};
25+
26+
for (let i = 0; i < nodes.length; i += nodeFieldCount) {
27+
const typeIdx = nodes[i + typeOffset];
28+
const typeName = nodeTypes[typeIdx];
29+
const nameIdx = nodes[i + nameOffset];
30+
const name = typeof nameIdx === 'number' ? strings[nameIdx] : nameIdx;
31+
const size = nodes[i + sizeOffset];
32+
33+
// Ignore native primitives/arrays that clutter the output unless specifically looking for them
34+
if (
35+
typeName === 'string' ||
36+
typeName === 'number' ||
37+
typeName === 'array'
38+
) {
39+
continue;
40+
}
41+
42+
const key = `${typeName}::${name}`;
43+
counts[key] = (counts[key] || 0) + 1;
44+
sizes[key] = (sizes[key] || 0) + size;
45+
}
46+
return {counts, sizes};
47+
}
48+
49+
const [, , file1, file2] = process.argv;
50+
if (!file1 || !file2) {
51+
console.error(
52+
'Usage: node compare_snapshots.js <baseline.heapsnapshot> <target.heapsnapshot>',
53+
);
54+
process.exit(1);
55+
}
56+
57+
try {
58+
const snap1 = parseSnapshot(file1);
59+
const snap2 = parseSnapshot(file2);
60+
61+
const diffs = [];
62+
for (const key in snap2.counts) {
63+
const count1 = snap1.counts[key] || 0;
64+
const count2 = snap2.counts[key];
65+
const size1 = snap1.sizes[key] || 0;
66+
const size2 = snap2.sizes[key];
67+
68+
if (count2 > count1) {
69+
diffs.push({
70+
key,
71+
countDiff: count2 - count1,
72+
sizeDiff: size2 - size1,
73+
});
74+
}
75+
}
76+
77+
diffs.sort((a, b) => b.sizeDiff - a.sizeDiff);
78+
79+
console.log('\n--- Top 10 growing objects by size ---');
80+
diffs.slice(0, 10).forEach(d => {
81+
console.log(`${d.key}: +${d.countDiff} objects, +${d.sizeDiff} bytes`);
82+
});
83+
84+
// Look for common leak indicators
85+
const commonLeaks = diffs.filter(
86+
d =>
87+
d.key.toLowerCase().includes('detached') ||
88+
d.key.toLowerCase().includes('html') ||
89+
d.key.toLowerCase().includes('eventlistener') ||
90+
d.key.toLowerCase().includes('context') ||
91+
d.key.toLowerCase().includes('closure'),
92+
);
93+
94+
commonLeaks.sort((a, b) => b.countDiff - a.countDiff);
95+
96+
console.log('\n--- Top 3 most common types of memory leaks found ---');
97+
if (commonLeaks.length === 0) {
98+
console.log('No common DOM or Closure leaks detected.');
99+
} else {
100+
commonLeaks.slice(0, 3).forEach(d => {
101+
console.log(`${d.key}: +${d.countDiff} objects, +${d.sizeDiff} bytes`);
102+
});
103+
}
104+
} catch (error) {
105+
console.error(
106+
'Error parsing snapshots. They might be too large for JSON.parse or invalid.',
107+
);
108+
console.error(error.message);
109+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Using Memlab
2+
3+
[Memlab](https://facebook.github.io/memlab/) is an E2E testing and analysis framework for finding JavaScript memory leaks.
4+
5+
## Important Rule
6+
7+
**NEVER read raw `.heapsnapshot` files directly.** They are too large and will exceed context limits. Always use `memlab` commands to analyze them.
8+
9+
## Analyzing Snapshots
10+
11+
You can use the `take_memory_snapshot` tool provided by the `chrome-devtools-mcp` extension to generate heap snapshots during an investigation. To find leaks, you generally need 3 snapshots:
12+
13+
1. **Baseline:** Before the suspect action.
14+
2. **Target:** After the suspect action.
15+
3. **Final:** After reverting the suspect action (e.g., closing a modal, navigating away).
16+
17+
Once you have these 3 snapshots saved to disk, you can use `memlab` to find leaks:
18+
19+
```bash
20+
npx memlab find-leaks --baseline <path-to-baseline> --target <path-to-target> --final <path-to-final>
21+
```
22+
23+
You can also parse a single snapshot to find the largest objects or explore it individually:
24+
25+
```bash
26+
npx memlab analyze snapshot --snapshot <path-to-snapshot>
27+
```
28+
29+
Memlab will output the retainer traces for identified leaks. Use these traces to guide your search in the codebase.

0 commit comments

Comments
 (0)