{"success":true,"name":"hunt-node-js-memory-leaks-by-owner-not-by-restarting","description":"Node.js process memory keeps growing or crashes with heap errors.","category":"ai-brimind","path":"ai-brimind/hunt-node-js-memory-leaks-by-owner-not-by-restarting/SKILL.md","file":"SKILL.md","content":"---\nname: hunt-node-js-memory-leaks-by-owner-not-by-restarting\ndescription: \"Node.js process memory keeps growing or crashes with heap errors.\"\n---\n\n# Hunt Node.js Memory Leaks by Owner, Not by Restarting\n\n## When to use\nProject is Node.js, and memory climbs over hours/days, you see 'heap out of memory', or you're tempted to schedule restarts as the fix.\n\n## Example request\nNightly restart cron 'fixes' the crash → that masks the symptom; something is accumulating — find the owner. A module-level Map cache with no size limit → unbounded growth by design — bounded LRU or TTL is the pattern. Event listeners added per request, never removed → the MaxListenersExceededWarning was the clue — remove listeners or use once. Heap snapshot shows thousands of identical objects → whatever retains them is the leak — compare two snapshots to see what grows.\n\n## Instructions\nAct as a Node.js debugger who refuses to accept 'restart it nightly' as a fix — growth means something has an owner that never lets go. First, confirm the leak with evidence: track process.memoryUsage() over time or take two heap snapshots (Chrome DevTools via --inspect) minutes apart under load and diff what grows. Then classify against the usual suspects in order: unbounded module-level Maps/arrays/caches, event listeners added repeatedly without removal (the MaxListenersExceededWarning is a smoking gun), closures held by timers that are never cleared, and per-request data pushed into globals. Name the owner: WHAT holds the reference, and WHY is it never released? Fix at the owner: bounded caches (LRU with max size or TTL), removeListener/once, clearInterval on shutdown paths. Never sprinkle global.gc() or raise --max-old-space-size as a cure — that only delays the crash. Only close the issue once a re-measurement CONFIRMS memory plateaus under the same load, and explain what was retaining the objects and why it no longer does.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #990\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}