heap-snapshot-analysis
Skill175 starsChanged 8 months agoMITView on GitHub
Analyze V8 heap snapshots to investigate memory leaks and retention issues. Use when given .heapsnapshot files, asked to compare before/after snapshots, asked to find what retains objects, or investigating why objects survive GC. Provides snapshot parsing, comparison, retainer-path helpers, and scratchpad scripts.
6 other projects use a version of this, 1 of them edited.
Adopted by
Steady: 0 new projects took it in the last 4 weeks, none the 4 weeks before.thin data
Most recently taken up by
- microsoft/vscodeword for word, first seen Nov 2, 2026
- NeuralInverse/neuralinverseword for word, first seen Oct 8, 2026
- posit-dev/positronword for word, first seen Oct 8, 2026
- xAmirHamza77/OmniCodeword for word, first seen May 21, 2026
- cpkt9762/opencode-vscode-ideedited, first seen Mar 31, 2026
Other versions6 projects use a version of this.
Word for word · 5
- microsoft/vscode★ 195k100% same
- posit-dev/positron★ 4.3k100% same
- NeuralInverse/neuralinverse★ 221100% same
- xAmirHamza77/OmniCode★ 12100% same
- 0-draft/zeus★ 1100% same
Edited · 1
- cpkt9762/opencode-vscode-ide★ 1799% same
Wrote their own version · 0
None found.
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository23 other files it gives its agents.
- AGENTS.mdAGENTS.md
- .github/copilot-instructions.mdCopilot instructions
- launchSkill
- accessibilitySkill
- add-policySkill
- agent-sessions-layoutSkill
- author-contributionsSkill
- auto-perf-optimizeSkill
- azure-pipelinesSkill
- chat-customizations-editorSkill
- chat-perfSkill
- component-fixturesSkill
- cpu-profile-analysisSkill
- fix-ci-failuresSkill
- fix-errorsSkill
- hygieneSkill
- integration-testsSkill
- memory-leak-auditSkill
- sessionsSkill
- tool-rename-deprecationSkill
- unit-testsSkill
- update-screenshotsSkill
- vscode-dev-workbenchSkill
The file
Show its rules and text
We split this skill into the rules it gives an agent
15 rules. About 0 are common, 9 are distinctive, and 6 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈9
Distinctive · estimate
This author's own choices. Worth a look.
6
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 12
Start every investigation fresh: Do NOT read, consult, or be influenced by prior investigations found in:
- Distinctive: line 30
If the user needs the agent to launch VS Code, drive a scenario, and capture snapshots first, use the VS Code performance workflow skill …
- Very specific: line 34
Use the helpers in parseSnapshot.ts to load snapshots.
- Distinctive: line 47
When a snapshot is too large to load into a single Buffer, write scratchpad scripts that scan and parse only the sections needed for the …
- Very specific: line 54
Avoid materializing the full strings table unless the investigation truly needs it.
- Distinctive: line 55
If you do need many strings, store only short previews and category counters.
- Distinctive: line 56
Write intermediate outputs to files in the scratchpad.
- Distinctive: line 57
Prefer self-size attribution and field-level ownership for huge graphs.
- Distinctive: line 58
When quantifying a suspected owner, count obvious owned fields separately: wrapper object, key arrays, array elements, direct strings, and …
- Very specific: line 85
Use compareSnapshots.ts to diff two snapshots:
- Very specific: line 96
Use findRetainers.ts to trace why an object is alive:
- Distinctive: line 107
Write investigation-specific scripts in the scratchpad directory.
- Very specific: line 173
Caveat: Don't null fields on viewmodel items (ChatResponseViewModel._model).
- Very specific: line 179
GCBasedDisposableTracker(FinalizationRegistry): If register(target, held, target) is used (target === unregister token), creates a … - Distinctive: line 184
All helper scripts use ESM and need Node with extra memory:
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Heap Snapshot Analysis
Investigate memory leaks from V8 heap snapshots (.heapsnapshot files). This skill starts when snapshots already exist: either the user provided them, DevTools exported them, or another workflow produced them. Use the helpers here to compare snapshots, group object deltas, and trace retainer paths.
IGNORE Prior Investigations
Start every investigation fresh. Do NOT read, consult, or be influenced by prior investigations found in:
/memories/(user, session, or repo memory).github/skills/heap-snapshot-analysis/scratchpad/(previous dated subfolders and theirfindings.mdfiles)- Any other notes from earlier sessions
Previous findings can bias the analysis toward suspects that are no longer relevant, or cause the agent to skip steps and jump to conclusions. Let the current snapshots speak for themselves. Only reference prior work if the user explicitly asks you to.
When to Use
- User provides
.heapsnapshotfiles (before/after a workflow) - User has heap snapshots captured by another skill or script
- Need to find what retains disposed objects (retainer path analysis)
- Comparing object counts/sizes between two snapshots
- Investigating why particular objects survive GC
Workflow
If the user needs the agent to launch VS Code, drive a scenario, and capture snapshots first, use the VS Code performance workflow skill before returning here for low-level snapshot analysis.
1. Parse Snapshots
Use the helpers in parseSnapshot.ts to load snapshots. The files are often >500MB and too large for JSON.parse as a string — the helpers use Buffer-based extraction. In scratchpad scripts, import helpers from ../helpers/*.ts.
For very large snapshots, the helper may still be too eager. Node cannot create a Buffer larger than roughly 2 GiB, so snapshots above that size can fail with ERR_FS_FILE_TOO_LARGE even before parsing. In that case, do not try to raise --max-old-space-size and retry the same full-file read. Switch to a streaming script.
import { parseSnapshot, buildGraph } from '../helpers/parseSnapshot.ts';
const data = parseSnapshot('/path/to/snapshot.heapsnapshot');
const graph = buildGraph(data);Snapshots Larger Than 2 GiB
When a snapshot is too large to load into a single Buffer, write scratchpad scripts that scan and parse only the sections needed for the question. Use streamSnapshot.mjs for the common streaming primitives instead of copying them between scratch scripts.
Useful tricks:
- Find top-level section offsets first. Scan the file as bytes for markers like
"nodes":,"edges":,"strings":, and"trace_function_infos":. This lets follow-up scripts jump directly to the large arrays instead of searching the whole file repeatedly. - Parse
snapshot.metaseparately from the small header at the start of the file. Usemeta.node_fields,meta.node_types,meta.edge_fields, andmeta.edge_typesto avoid hard-coding tuple widths. - Stream numeric arrays in chunks. For
nodesandedges, keep a small carryover string between chunks, split on commas, and process complete numeric tokens as they arrive. - Avoid materializing the full
stringstable unless the investigation truly needs it. If you only need suspicious names, collect string indexes from matching nodes/edges first, then resolve only those indexes in a second streaming pass. - If you do need many strings, store only short previews and category counters. Full source strings, ref-listing strings, and prompt payloads can dominate memory and make the analyzer become the leak.
- Write intermediate outputs to files in the scratchpad. Large heap analysis is iterative and slow; cached node ids, offsets, and retainer traces save repeated multi-minute passes.
- Prefer self-size attribution and field-level ownership for huge graphs. Full retained-size walks can wildly overcount shared services, roots, maps, and singleton caches.
- When quantifying a suspected owner, count obvious owned fields separately: wrapper object, key arrays, array elements, direct strings, and parent strings of sliced/concatenated strings. This often gives a better lower-bound than a single direct string bucket.
- Be explicit about approximation boundaries. A field-level subtotal usually undercounts listeners/watchers/back-references but avoids the much worse problem of attributing the whole runtime to one object.
Example large-snapshot workflow:
import { findArrayStart, findTokenOffsets, parseMeta, streamNumberTuples } from '../../helpers/streamSnapshot.mjs';
const { size, offsets } = findTokenOffsets(snapshotPath);
const meta = parseMeta(snapshotPath);
const nodeFieldCount = meta.node_fields.length;
const nodesStart = findArrayStart(snapshotPath, offsets.get('"nodes"'));
streamNumberTuples(snapshotPath, nodesStart, offsets.get('"edges"'), nodeFieldCount, (node, nodeIndex) => {
// node is reused for speed; copy it before storing.
});cd .github/skills/heap-snapshot-analysis
node --max-old-space-size=24576 scratchpad/YYYY-MM-DD-topic/findOffsets.mjs /path/to/Heap.heapsnapshot
node --max-old-space-size=24576 scratchpad/YYYY-MM-DD-topic/streamAnalyze.mjs /path/to/Heap.heapsnapshot > scratchpad/YYYY-MM-DD-topic/streamAnalyze.out
node --max-old-space-size=24576 scratchpad/YYYY-MM-DD-topic/traceNodes.mjs /path/to/Heap.heapsnapshot 12345 67890 > scratchpad/YYYY-MM-DD-topic/traceNodes.out2. Compare Before/After
Use compareSnapshots.ts to diff two snapshots:
import { compareSnapshots } from '../helpers/compareSnapshots.ts';
const result = compareSnapshots('/path/to/before.heapsnapshot', '/path/to/after.heapsnapshot');
// result.topBySize, result.topByCount, result.newObjectGroups, result.summary3. Find Retainer Paths
Use findRetainers.ts to trace why an object is alive:
import { findRetainerPaths } from '../helpers/findRetainers.ts';
// Find what keeps ChatModel instances alive (skipping weak edges)
findRetainerPaths(graph, 'ChatModel', { maxPaths: 5, maxDepth: 25, maxAttempts: 200 });4. Write Investigation Scripts
Write investigation-specific scripts in the scratchpad directory. This folder is gitignored — use it freely for one-off analysis.
Organize scratchpad work into dated subfolders named YYYY-MM-DD-short-description/ (e.g., 2026-04-09-chat-model-retainers/). Each subfolder should contain:
- The analysis scripts (
.mjs,.mts, etc.) - A
findings.mdfile documenting the full investigation: all ideas considered, which ones led to changes and which were rejected (and why), before/after measurements, and a summary of the outcome. This lets the user review the agent's reasoning, decide which changes to keep, and follow up on deferred ideas.
Scripts can import the helpers:
cd .github/skills/heap-snapshot-analysis
node --max-old-space-size=16384 scratchpad/2026-04-09-chat-model-retainers/analyze.mjsKey Concepts
V8 Heap Snapshot Format
The .heapsnapshot file is JSON with these key sections:
snapshot.meta: Field definitions for nodes and edgesnodes: Flat array, every N values = one node (N =meta.node_fields.length, typically 6:type, name, id, self_size, edge_count, detachedness)edges: Flat array, every M values = one edge (M =meta.edge_fields.length, typically 3:type, name_or_index, to_node)strings: String table indexed bynamefields in nodes/edges
Edge Types That Matter
| Type | Meaning | Prevents GC? |
property | Named JS property | Yes |
element | Array index | Yes |
context | Closure variable | Yes |
internal | V8 internal reference | Yes |
hidden | V8 hidden reference | Yes |
weak | WeakRef/WeakMap key | No |
shortcut | Convenience link | Depends |
Always skip weak edges when tracing retainer paths. WeakMap entries show up as edges from key → backing array, but they don't prevent collection — they're red herrings.
Common VS Code Retention Patterns
- RowCache templates: ListView's
RowCachestores template rows. Templates havecurrentElementpointing to old viewmodel items. If not cleared on session switch, retains entire model chains. - Resource pools:
pool.clear()only disposes idle items. If_onDidUpdateViewModel.fire()runs AFTERpool.clear(), released items re-enter the empty pool and are never disposed. Fire event first, then clear. autorunIterableDeltalastValues: The closure captures aMapof previous iteration values. Values stay until the autorun re-runs. Async disposal delays keep models in observable stores longer than expected.HoverService._delayedHovers: Global singleton Map retaining disposed objects viashowclosure →resolveHoverOptionsclosure →this. If hover cleanup disposable doesn't fire, the entire object tree is retained.ObjectMutationLog._previous: The incremental serializer keeps a full snapshot of the last-serialized state. Every loaded ChatModel holds 2x its data: live +_previous._previousModelRefpattern:MutableDisposablesetter disposes the old value. Reading.valueand storing it elsewhere, then setting.value = undefined, disposes the stored reference. UseclearAndLeak()to extract without disposing.
Defensive Nulling
Null heavy fields in dispose() to break retention chains even when something retains the disposed object:
override dispose() {
super.dispose();
this._requests.length = 0; // conversation data
this.dataSerializer = undefined; // serialization snapshot
this._editingSession = undefined; // editing session + TextModels
this._session = undefined!; // back-reference cycles
}Caveat: Don't null fields on viewmodel items (ChatResponseViewModel._model). The tree's diffIdentityProvider accesses them after the parent viewmodel is disposed but before setChildren replaces them.
False Retainers to Watch For
- DevTools debugger global handles: If the snapshot was captured after opening DevTools, large source strings, compiled scripts, preview data, inspected objects, or debugger bookkeeping can be retained by paths like
DevTools debugger(internal)→synthetic::(Global handles)→ GC roots. Treat these as debugger-induced until proven otherwise. They may not exist in the app before DevTools opens, and they should not be confused with application-owned leaks. DevToolsLogger._aliveInstances(Map): Enabled byVSCODE_DEV_DEBUG_OBSERVABLESenv var. Retains ALL observed observables. Check if this is active before investigating observable-rooted paths.GCBasedDisposableTracker(FinalizationRegistry): Ifregister(target, held, target)is used (target === unregister token), creates a strong self-reference preventing GC. Currently commented out in production.- WeakMap backing arrays: Show up in retainer paths but don't prevent collection.
Running Analysis
All helper scripts use ESM and need Node with extra memory:
node --max-old-space-size=16384 scratchpad/analyze.mjsTypical analysis takes 30-120 seconds per snapshot depending on size.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

