memlab-find-web-leaks
ما الذي تفعله
يكتب سيناريو memlab يفتح صفحة، وينفذ تفاعلا واحدا، ويتراجع عنه، ثم يبلغ عن الكائنات التي تبقى في الذاكرة. وهي مخصصة لمطوري الويب الذين يحتاجون إلى فحص نافذة منبثقة أو مسار تنقل أو تدفق مستخدم بحثا عن تسريبات.
متى تستخدمها
- ✓عندما تريد فحص صفحة أو نافذة حوار بحثا عن تسريبات الذاكرة.
- ✓عندما يبدو أن تدفق المستخدم يستهلك ذاكرة أكثر في كل مرة يعمل فيها.
- ✓عندما تريد تقارير تسريب تشير إلى الكود الذي يُبقي الكائنات حية.
رابط المهارة
https://github.com/meta/memlab/blob/main/plugins/memlab/skills/memlab-find-web-leaks/SKILL.mdانسخه والصقه في الذكاء الاصطناعي الذي تستخدمه (Claude أو ChatGPT أو Cursor) واطلب منه اتباع التعليمات.
طريقة الاستخدام
أسهل طريقة: انسخ الرسالة والصقها في الذكاء الاصطناعي الذي تستخدمه. سيقرأ المهارة وينفّذ تعليماتها.
رسالة جاهزة للصق
لـ Claude Code أو Codex أو Cursor: شغّله داخل مجلد مشروعك وستُثبَّت المهارة.
نزّل ملف SKILL.md وضعه في مجلد مهارات وكيلك.
مثال على ما تطلبه من الذكاء الاصطناعي
“تحقق مما إذا كان فتح نافذة الإعدادات وإغلاقها في تطبيق React الخاص بي يسرّب الذاكرة، وأصلح أي تسريب تجده.”
5 مهارات جديدة كل أسبوع. قراءة في دقيقة واحدة.
كيف تعمل
يكتب وكيلك سيناريو يتضمن صفحة وإجراء واحدا وخطوة تراجع دقيقة، ثم يشغّل memlab. ويقرأ مسارات التسريب المجمعة ويصلح الكود الذي يُبقي عناصر DOM المنفصلة أو مكونات React غير المركّبة حية.
المحتوى التقني (ملف 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
التعليقات
- …
المزيد من المهارات من هذا المستودع
memlab-analyze-heap-snapshot
5.1kيساعد وكيلك على إيجاد تسريبات الذاكرة في تطبيق JavaScript من خلال تحليل لقطة الكومة، وشرح ما يبقي الكائنات حية.
memlab-capture-heap-snapshots
5.1kيرشد وكيلك في التقاط لقطات كومة JavaScript من المتصفح أو Node.js أو Electron أو React Native، لتدرس استهلاك الذاكرة.
memlab-memory-tests
5.1kيساعد وكيلك على كتابة اختبارات آلية تكشف تسريبات الذاكرة، سواء لكود Node.js أو لتدفقات المتصفح التي تعمل في التكامل المستمر.
مهارات مشابهة
test-driven-development
311kيعلّم وكيلك كتابة اختبار فاشل أولًا، ثم رؤيته يفشل، ثم كتابة أصغر شيفرة تنجحه، قبل بناء أي ميزة أو إصلاح.
verification-before-completion
311kيعلّم وكيلك تشغيل فحوصات حقيقية وقراءة نتائجها قبل أن يقول إن المهمة أو الإصلاح أو البناء مكتمل أو ناجح.
migrate-to-shoehorn
310kيستبدل تأكيدات النوع الخطرة في ملفات الاختبار بمكتبة shoehorn، حتى تتمكن الاختبارات من استخدام بيانات جزئية وتجتاز فحوص الأنواع.