memory-leak-detection — Claude Code skill: find and fix leaks
A skill for tracking down memory leaks: heap snapshots, profiling and the usual patterns behind growing memory use.
npx skills add aj-geddes/useful-ai-prompts --skill memory-leak-detection -a claude-codeWhat memory-leak-detection does
The skill walks through leak hunting step by step. It takes heap snapshots with v8.writeHeapSnapshot, reads process.memoryUsage() and prints RSS, heap total, heap used and external in megabytes. A MemoryProfiler class in TypeScript ships as a starting point you can drop into your own code.
Beyond the quick example, the skill points to guides in the references/ directory: Node.js heap snapshots, leak detection middleware, common leak patterns, Python memory profiling, WeakMap and WeakRef caches, and memory monitoring in production. The practices section is blunt about cleanup — remove event listeners, clear timers and intervals, cap cache sizes, profile on a schedule.
Reach for it when memory climbs over time, containers keep restarting, or the process dies with an OOM error. The flip side is covered too: circular references, stray references to large objects and forgotten resource cleanup are exactly what causes the leak in the first place.
What it helps with
- trace why memory keeps growing in a Node.js service
- read a heap snapshot and find what still holds objects
- fix a leak caused by event listeners that never get removed
- swap a plain object cache for WeakMap and bound its size
- set up memory monitoring in production
Example prompts for the agent
- take a heap snapshot and show me what is growing
- find the memory leak in this Node.js service
- check whether timers or listeners are holding references
How to install memory-leak-detection
1Skills CLI (Vercel)
npx skills add aj-geddes/useful-ai-prompts --skill memory-leak-detection -a claude-code2Manually: put the files in place
~/.claude/skills/memory-leak-detection/SKILL.mdCopy the whole skill folder; for one project use .claude/skills/memory-leak-detection/
1Skills CLI (Vercel)
npx skills add aj-geddes/useful-ai-prompts --skill memory-leak-detection -a codex2Manually: put the files in place
.agents/skills/memory-leak-detection/SKILL.mdFor all projects use ~/.agents/skills/
1Skills CLI (Vercel)
npx skills add aj-geddes/useful-ai-prompts --skill memory-leak-detection -a cursor2Manually: put the files in place
.cursor/skills/memory-leak-detection/SKILL.mdCursor also reads .agents/skills/ and .claude/skills/
1GitHub CLI
gh skill install aj-geddes/useful-ai-prompts memory-leak-detection2Manually: put the files in place
.github/skills/memory-leak-detection/SKILL.md1Skills CLI (Vercel)
npx skills add aj-geddes/useful-ai-prompts --skill memory-leak-detection -a gemini-cli2Platform CLI
gemini skills install https://github.com/aj-geddes/useful-ai-prompts --path skills/memory-leak-detectionManually: put the files in place
.devin/skills/memory-leak-detection/SKILL.mdThe legacy .windsurf/skills/ path still works
Skills CLI (Vercel)
npx skills add aj-geddes/useful-ai-prompts --skill memory-leak-detection -a opencodeClaude.ai
Zip the folder and upload it in Settings → Capabilities → Skills
Step by step, with commands for each platform: How to install skills in Claude Code: five ways
Questions about memory-leak-detection
How do I install memory-leak-detection in Claude?
Run npx skills add aj-geddes/useful-ai-prompts --skill memory-leak-detection -a claude-code. The skill file then lives at ~/.claude/skills/memory-leak-detection/SKILL.md.
What does memory-leak-detection do?
It helps you detect and fix memory leaks using heap snapshots, memory profiling, common leak patterns and production memory monitoring.
Which platforms can run this skill?
Besides Claude, it installs for Codex, Cursor, GitHub Copilot, Gemini CLI and OpenCode — the commands differ only in the agent flag.
Source text
SKILL.md · MIT
---
name: memory-leak-detection
description: >
Detect and fix memory leaks using heap snapshots, memory profiling, and leak
detection tools. Use when investigating memory growth, OOM errors, or
optimizing memory usage.
---
# Memory Leak Detection
## Table of Contents
- [Overview](#overview)
- [When to Use](#when-to-use)
- [Quick Start](#quick-start)
- [Reference Guides](#reference-guides)
- [Best Practices](#best-practices)
## Overview
Identify and fix memory leaks to prevent out-of-memory crashes and optimize application performance.
## When to Use
- Memory usage growing over time
- Out of memory (OOM) errors
- Performance degradation
- Container restarts
- High memory consumption
## Quick Start
Minimal working example:
```typescript
import v8 from "v8";
import fs from "fs";
class MemoryProfiler {
takeSnapshot(filename: string): void {
const snapshot = v8.writeHeapSnapshot(filename);
console.log(`Heap snapshot saved to ${snapshot}`);
}
getMemoryUsage(): NodeJS.MemoryUsage {
return process.memoryUsage();
}
formatMemory(bytes: number): string {
return `${(bytes / 1024 / 1024).toFixed(2)} MB`;
}
printMemoryUsage(): void {
const usage = this.getMemoryUsage();
console.log("Memory Usage:");
console.log(` RSS: ${this.formatMemory(usage.rss)}`);
console.log(` Heap Total: ${this.formatMemory(usage.heapTotal)}`);
console.log(` Heap Used: ${this.formatMemory(usage.heapUsed)}`);
console.log(` External: ${this.formatMemory(usage.external)}`);
// ... (see reference guides for full implementation)
```
## Reference Guides
Detailed implementations in the `references/` directory:
| Guide | Contents |
|---|---|
| [Node.js Heap Snapshots](references/nodejs-heap-snapshots.md) | Node.js Heap Snapshots |
| [Memory Leak Detection Middleware](references/memory-leak-detection-middleware.md) | Memory Leak Detection Middleware |
| [Common Memory Leak Patterns](references/common-memory-leak-patterns.md) | Common Memory Leak Patterns |
| [Python Memory Profiling](references/python-memory-profiling.md) | Python Memory Profiling |
| [WeakMap/WeakRef for Cache](references/weakmapweakref-for-cache.md) | WeakMap/WeakRef for Cache |
| [Memory Monitoring in Production](references/memory-monitoring-in-production.md) | Memory Monitoring in Production |
## Best Practices
### ✅ DO
- Remove event listeners when done
- Clear timers and intervals
- Use WeakMap/WeakRef for caches
- Limit cache sizes
- Monitor memory in production
- Profile regularly
- Clean up after tests
### ❌ DON'T
- Create circular references
- Hold references to large objects unnecessarily
- Forget to clean up resources
- Ignore memory growth
- Skip WeakMap for object caches
© aj-geddes · MIT · aj-geddes/useful-ai-prompts