跳到正文
Skillpedia
测试5.1k · #15057

memlab-find-web-leaks

它能做什么

它编写一个 memlab 场景:打开页面、执行一次交互、撤销该交互,并报告仍留在内存中的对象。适合需要检查弹窗、对话框、路由或用户流程是否存在泄漏的网页开发者。

适用场景

  • ✓当你想检查某个页面或对话框是否存在内存泄漏时。
  • ✓当某个用户流程每次运行都占用更多内存时。
  • ✓当你想获得能指向使对象保持存活的代码的泄漏报告时。

技能链接

https://github.com/meta/memlab/blob/main/plugins/memlab/skills/memlab-find-web-leaks/SKILL.md

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

使用方法

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

可直接粘贴的消息

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

可以这样问 AI

“请检查我 React 应用中打开和关闭设置对话框是否会造成内存泄漏,并修复发现的任何泄漏。”

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

工作原理

智能体编写一个包含页面、操作和精确撤销步骤的场景,然后运行 memlab。它会读取分组后的泄漏追踪信息,并修复使分离 DOM 或已卸载组件保持存活的代码。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Find memory leaks in a web app with memlab

memlab loads the page, takes a heap snapshot, runs the interaction you suspect (action), undoes it (back), and takes two more snapshots. Objects that action allocated and that are still alive after back are leak candidates. By default memlab reports detached DOM elements and unmounted React Fiber nodes, grouped into clusters by retainer trace.

1. Install

npm install -g memlab   # or prefix every command with npx: npx memlab run ...
memlab version

2. Write the scenario

Save a file such as scenario.js. Pick one interaction, and make back its exact inverse:

// scenario.js
module.exports = {
  // Page to load first. memlab takes the baseline snapshot here.
  url: () => 'http://localhost:3000/',

  // The interaction suspected of leaking. Target snapshot after this.
  action: async page => {
    await page.click('[data-testid="open-settings"]');
    await page.waitForSelector('[data-testid="settings-dialog"]');
  },

  // Undo the interaction without reloading. Final snapshot after this.
  back: async page => {
    await page.click('[data-testid="close-settings"]');
    await page.waitForSelector('[data-testid="settings-dialog"]', {hidden: true});
  },
};

Rules that keep the result valid:

  • No reloads in action or back. page.goto(), page.reload(), location.reload() or a link that does a full page load start a new heap. memlab detects this and stops with "The page is reloaded. MemLab cannot analyze heap across page reloads." Client-side route changes are fine.
  • back must really undo action. If the dialog stays open, its objects are still in use and nothing is reported (or everything is).
  • page is a Puppeteer Page. memlab 2.x uses Puppeteer 24: only use methods from the Puppeteer API. page.waitForXPath() no longer exists. Puppeteer locators have no count() or first(); use page.$$(selector) to count elements.
  • await every Puppeteer call.
  • Log in with cookies (or in setup), not in action. Work done in action that back doesn't undo shows up as leaks.
  • Keep it minimal. Add other callbacks only when needed. repeat is only for stress tests.

All scenario callbacks, with examples, are in references/scenario-api.md.

3. Run

memlab run --scenario scenario.js

When it fails or gets stuck, add --verbose for stack traces, --headful to watch the browser, or --debug to pause after each step. Leave them off otherwise. --work-dir <dir> saves the run somewhere other than the default working directory (memlab get-default-work-dir).

4. Read the result

MemLab found 1 leak(s)
--Similar leaks in this run: 4--
--Retained size of leaked objects: 8.3MB--
[Window] (native) @35847 [8.3MB]
  --20 (element)--->  [InternalNode] (native) @130981728 [8.3MB]
  --1 (element)--->  [EventListener] (native) @131009888 [8.3MB]
  --1 (element)--->  [V8EventListener] (native) @224808192 [8.3MB]
  --1 (element)--->  [eventHandler] (closure) @168079 [8.3MB]
  --context (internal)--->  [<function scope>] (object) @181905 [8.3MB]
  --bigArray (variable)--->  [Array] (object) @182925 [8.3MB]
  • Each block is a cluster of leaks with similar retainer traces, sorted by retained size. "Similar leaks" is how many leaked objects share it.
  • A trace starts at a GC root and ends at the leaked object. Each line is --edge (edge type)---> [node name] (node type) @id [retained size].
  • Find the link to break: scan for the first names that come from the app (a closure, property or variable name) rather than the browser or the framework. Above, the eventHandler closure is still registered as an event listener, and its scope holds bigArray. The fix is to remove the listener when the UI closes.
  • Traces are much easier to read when the app serves unminified code or keeps readable names.
  • The text report is saved in the working directory as data/out/leaks.txt. memlab run also copies it to data/cur/leaks.txt, next to the three snapshots s1–s3.heapsnapshot. Runs through @memlab/api leave it only in data/out/.

5. When the trace isn't enough

  • memlab trace --node-id <id>: the retainer trace for one object.
  • memlab view-heap --snapshot <file>: interactive heap browser.
  • Nothing reported but memory still grows? The built-in detector
常见问题

为什么撤销步骤必须精确?

撤销步骤应当准确地还原操作,这样事后仍然存活的对象才能被判定为泄漏。

场景会重新加载页面吗?

不会。该技能规定,在操作步骤和撤销步骤期间不要重新加载页面。

默认会报告哪些对象?

分离的 DOM 元素和已卸载的 React 组件。

详情

作者的原始描述

Find memory leaks in a web app with memlab. Write a test scenario (url, action, back) that drives the page in Chromium through Puppeteer, run `memlab run`, read the clustered leak traces, and fix the code that keeps detached DOM elements, unmounted React components or other objects alive. Use when the user asks to check a page, component, modal, dialog, route or user flow for memory leaks, to write a memlab scenario, to set up memory leak tests for a web app, or to find out why memory is not released after a UI interaction is undone.

repo: meta/memlab

热度

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

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能