memlab-find-web-leaks
यह क्या करती है
यह memlab परिदृश्य (scenario) लिखती है जो पेज खोलता है, एक इंटरैक्शन चलाता है, उसे पलटता है, और उन ऑब्जेक्ट की रिपोर्ट देता है जो मेमोरी में बचे रह जाते हैं। यह उन वेब डेवलपर्स के लिए है जिन्हें मॉडल, डायलॉग, रूट या यूज़र फ़्लो में लीक जाँचना है।
कब इस्तेमाल करें
- ✓जब आप किसी पेज या डायलॉग में मेमोरी लीक जाँचना चाहते हैं।
- ✓जब कोई यूज़र फ़्लो हर बार चलाने पर ज़्यादा मेमोरी लेता लगे।
- ✓जब आपको ऐसी लीक रिपोर्ट चाहिए जो उस कोड की ओर इशारा करे जो ऑब्जेक्ट ज़िंदा रख रहा है।
Skill का लिंक
https://github.com/meta/memlab/blob/main/plugins/memlab/skills/memlab-find-web-leaks/SKILL.mdइसे कॉपी करके अपने AI (Claude, ChatGPT या Cursor) में पेस्ट करें और निर्देशों का पालन करने को कहें।
कैसे इस्तेमाल करें
सबसे आसान तरीका: मैसेज कॉपी करके अपने AI में पेस्ट करें। वह skill पढ़कर निर्देशों का पालन करेगा।
पेस्ट करने के लिए तैयार मैसेज
Claude Code, Codex या Cursor के लिए: प्रोजेक्ट फ़ोल्डर में चलाएँ और skill इंस्टॉल हो जाएगी।
SKILL.md डाउनलोड करें और अपने एजेंट के skills फ़ोल्डर में रखें।
AI से क्या माँगें: उदाहरण
“जाँचिए कि मेरे React ऐप में सेटिंग्स डायलॉग खोलने और बंद करने से मेमोरी लीक होती है या नहीं, और जो लीक मिले उसे ठीक कीजिए।”
हर हफ़्ते 5 नई skills, 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
actionorback.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. backmust really undoaction. If the dialog stays open, its objects are still in use and nothing is reported (or everything is).pageis a PuppeteerPage. memlab 2.x uses Puppeteer 24: only use methods from the Puppeteer API.page.waitForXPath()no longer exists. Puppeteer locators have nocount()orfirst(); usepage.$$(selector)to count elements.awaitevery Puppeteer call.- Log in with
cookies(or insetup), not inaction. Work done inactionthatbackdoesn't undo shows up as leaks. - Keep it minimal. Add other callbacks only when needed.
repeatis 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
eventHandlerclosure is still registered as an event listener, and its scope holdsbigArray. 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 runalso copies it todata/cur/leaks.txt, next to the three snapshotss1–s3.heapsnapshot. Runs through@memlab/apileave it only indata/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
- अपडेट की गई
- 31/10/2026
कमेंट
- …
इस repository की और skills
memlab-analyze-heap-snapshot
5.1kयह आपके एजेंट को heap snapshot पढ़कर JavaScript ऐप में मेमोरी लीक ढूँढने और समझाने में मदद करती है कि कौन-सी चीज़ें ऑब्जेक्ट को ज़िंदा रखती हैं।
memlab-capture-heap-snapshots
5.1kयह आपके एजेंट को ब्राउज़र, Node.js, Electron या React Native से JavaScript heap snapshot लेने में मार्गदर्शन देती है, ताकि आप मेमोरी उपयोग का अध्ययन कर सकें।
memlab-memory-tests
5.1kयह आपके एजेंट को ऐसे स्वचालित टेस्ट लिखने में मदद करती है जो मेमोरी लीक पकड़ें: Node.js कोड के लिए, या CI में चलने वाले ब्राउज़र फ़्लो के लिए।
मिलती-जुलती skills
test-driven-development
311kयह आपके एजेंट को किसी भी फ़ीचर या सुधार से पहले पहले असफल होने वाला टेस्ट लिखना, उसे असफल होते देखना, फिर पास करने वाला सबसे छोटा कोड लिखना सिखाती है।
verification-before-completion
311kयह आपके एजेंट को किसी काम, सुधार या build के पूरा या पास होने का दावा करने से पहले असली जांच चलाकर उसका आउटपुट पढ़ना सिखाती है।
migrate-to-shoehorn
310kआपकी टेस्ट फ़ाइलों में जोखिम भरे type casts की जगह shoehorn लाइब्रेरी लगाती है, ताकि टेस्ट अधूरे डेटा से भी type checks पास कर सकें।