- Home
- Skills
- Aj Geddes
- Useful Ai Prompts
- Memory Leak Detection
memory-leak-detection_skill
- Shell
73
GitHub Stars
1
Bundled Files
2 months ago
Catalog Refreshed
4 months ago
First Indexed
Readme & install
Copy the install command, review bundled files from the catalogue, and read any extended description pulled from the listing source.
Installation
Preview and clipboard use veilstrat where the catalogue uses aiagentskills.
npx veilstrat add skill aj-geddes/useful-ai-prompts --skill memory-leak-detection- SKILL.md8.5 KB
Overview
This skill detects and helps fix memory leaks using heap snapshots, runtime profiling, and common leak-detection patterns. It provides practical code patterns, monitoring middleware, and tooling guidance for Node.js and Python. Use it to prevent OOM crashes and reduce memory-driven performance regressions.
How this skill works
The skill guides you to collect runtime memory data (heap snapshots, tracemalloc snapshots, memory samples) and compare before/after states to find growth sources. It includes lightweight monitoring middleware that samples heap usage, alerts on sustained growth, and suggestions for fixes like removing listeners, clearing timers, or using WeakMap/WeakRef. It also outlines production monitoring and force-GC strategies when available.
When to use it
- Memory usage grows steadily over time or after specific flows
- Out-of-memory (OOM) errors or repeated container restarts
- Performance degradation correlated with memory increases
- Investigating high memory consumption in production
- Verifying memory behavior after deploying a change
Best practices
- Take and compare heap snapshots or tracemalloc snapshots before and after suspicious operations
- Remove event listeners, clear timers, and unbind callbacks during teardown
- Use WeakMap/WeakRef for caches that should not prevent GC
- Limit cache sizes with bounded eviction to avoid unbounded growth
- Monitor memory in production and alert on sustained heap growth
Example use cases
- Capture Node.js heap snapshots before and after a long-running job and compare in Chrome DevTools
- Add middleware that samples process.memoryUsage() and warns when heapUsed grows >50% over samples
- Use tracemalloc in Python to identify top allocation sites after a test scenario
- Replace a global accumulating array with a bounded cache or WeakMap to allow GC
- Deploy a memory monitor that triggers alerts and optional force GC when thresholds are exceeded
FAQ
Take incremental snapshots, compare allocations by file/line, and inspect retained objects and retaining paths in DevTools or tracemalloc output to trace back to the allocating code.
Can WeakMap/WeakRef always fix cache leaks?
They help when keys are objects and you want entries to be GC-able, but they don't solve leaks caused by global references, timers, or event listeners; use them alongside proper cleanup.