Debug a Memory Leak
Workflow for finding and fixing memory leaks
CLAUDE.md
When debugging a memory leak:
- Confirm the leak: monitor memory usage over time. A leak shows steadily increasing memory without plateauing.
- Take heap snapshots at intervals: before the operation, after repeating the operation several times.
- Compare snapshots to identify objects that accumulate but are never released.
- Check common causes: event listeners not removed, intervals/timers not cleared, closures holding references, growing caches without eviction, detached DOM nodes.
- Fix the leak: add cleanup functions, clear timers, remove event listeners, add cache size limits.
- Verify the fix by monitoring memory usage under the same load pattern.
Copy this workflow into your CLAUDE.md or agent config file so your agent follows this process automatically.