跳到正文
Skillpedia
通用编程5.1k · #15055

memlab-analyze-heap-snapshot

它能做什么

它使用 memlab 工具读取来自 Chrome、Node.js、Electron 或 React Native 的堆快照,并追踪内存为何持续增长。适合排查 JavaScript 应用内存问题或崩溃的开发者。

适用场景

  • ✓当你有一个堆快照文件,并想知道是什么在占用内存时。
  • ✓当你的 JavaScript 应用内存持续增长或耗尽时。
  • ✓当你怀疑浏览器应用中存在分离的 DOM 元素或不断增长的缓存时。

技能链接

https://github.com/meta/memlab/blob/main/plugins/memlab/skills/memlab-analyze-heap-snapshot/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/meta/memlab/blob/main/plugins/memlab/skills/memlab-analyze-heap-snapshot/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“这是我 Node 服务器的 app.heapsnapshot。请找出什么在内存中持续增长,并给出修复建议。”

每周 5 个新技能,1 分钟读完。

工作原理

智能体在 memlab 中加载快照,运行它的快速诊断和调查工具,并追踪使对象保持存活的引用链。然后,它会根据这些发现提出修复建议。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Analyze a JavaScript heap snapshot with memlab

memlab parses a V8 or Hermes heap snapshot into a queryable graph and answers questions about it: what fills the heap, what keeps growing, and which chain of references keeps an object alive.

1. Get the tools

  • MCP tools available (names start with memlab_): use them directly.

  • No MCP tools: use memlab-cli, which runs the same tools from a shell. One call runs several tools in one server session, so load the snapshot and analyze it in the same command:

    npx -y -p @memlab/mcp-server memlab-cli call \
      memlab_load_snapshot '{"file_path":"/abs/path/app.heapsnapshot"}' \
      memlab_quick_diagnosis '{}' \
      memlab_auto_investigate '{}'
    

    Each call starts a new server and reloads the snapshot. For an investigation with many steps, keep one server running instead; see references/memlab-cli.md.

  • If the user wants the MCP server set up permanently, suggest the memlab plugin, which also installs these skills: claude plugin marketplace add meta/memlab then claude plugin install memlab@memlab (Claude Code), or codex plugin marketplace add meta/memlab then codex plugin add memlab@memlab (Codex). For the server alone, give them claude mcp add memlab -e NODE_OPTIONS=--max-old-space-size=8192 -- npx -y -p @memlab/mcp-server memlab-mcp (Claude Code) or the JSON config in the MCP server docs. Keep --max-old-space-size=8192 or higher: without it, large snapshots are refused.

2. Get the snapshot

  • memlab_load_snapshot takes a local absolute path. If only <path>.gz exists, pass <path> and the server decompresses it. Download remote snapshots first.
  • For a file over about 1 GB, call memlab_snapshot_header first. It reads node and edge counts without parsing the graph and says whether the snapshot will load.
  • One snapshot shows what is in memory. To show what grows, you need two or more snapshots taken from the same page or process while an interaction repeats. If the user has none, see the memlab-capture-heap-snapshots skill or the memlab docs.

3. Triage

  1. memlab_load_snapshot with the path. Read the quick diagnosis it returns.
  2. In parallel (all analysis tools are read-only once a snapshot is loaded):
    • memlab_quick_diagnosis: summary, top objects, class histogram, duplicated strings.
    • memlab_auto_investigate: top retainers with retainer chains and severity, pinch points, unbounded caches, pending promises, known framework leak patterns.
  3. memlab_app_heap to split the heap into application data, bundle source and engine internals before blaming the app for the total.
  4. Note whether it is a browser or a Node.js heap; DOM tools only apply to browser snapshots.

4. Follow the matching path

Pick the path that fits the triage result. Each path is a short tool sequence in references/investigation-paths.md.

Triage shows Path
Many duplicated or huge strings A: String waste
Detached DOM elements (browser) B: DOM leaks
A class with very many instances, or one object retaining >5% of the heap C: Object accumulation
Large closures in retainer chains D: Closure leaks
Large non-built-in globals E: Global variables
Many callback/listener/handler objects F: Listener accumulation
Many identical Error objects G: Repeated errors
Metric, cache key or record field with huge value cardinality H: Cardinality explosion
Only one snapshot, need a growth guess I: Single-snapshot growth signals
Three or more snapshots in order J: Trend across snapshots
Huge detached tree in a browser snapshot, possibly from DevTools K: Real leak or dev artifact

5. Explain the retention

For the suspect object:

  • memlab_retainer_trace with its node_id: the shortest path from a GC root. Use expand: true when the interesting frame is hidden in an elided run.
  • memlab_retainer_layers with the same node_id before proposing a fix. It lists every independent retaining path; cutting one edge frees nothing if another path still holds the object.
  • memlab_closure_inspection when a closure (or a suspended async function) is in the chain: it shows the captured variables and their s
常见问题

在哪里获取堆快照?

另有一个单独的技能,涵盖从 Chrome、Edge、Node.js、Electron 或 React Native 中捕获快照。

它需要特殊设置吗?

如果未将 memlab 工具设置为服务器,该技能可以通过 npx 从命令行运行 memlab。

它会解释为什么某个对象一直留在内存中吗?

会。它会追踪使该对象保持存活的引用链。

详情

作者的原始描述

Analyze JavaScript heap snapshots (.heapsnapshot files from Chrome, Edge, Node.js, Electron or React Native/Hermes) with memlab to find memory leaks, explain why objects stay in memory, and suggest fixes. Use when the user shares or mentions a heap snapshot, asks why memory or heap size keeps growing, sees an out-of-memory crash, detached DOM elements, a growing cache or listener count, or wants to compare snapshots taken before and after a change. Works through the memlab MCP server tools (memlab_*) or, without MCP, the memlab-cli command from the @memlab/mcp-server npm package.

repo: meta/memlab

热度

GitHub 星标
5.1k
排名
#15055
许可证
MIT
更新时间
2026/10/31

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能