🐛

Systematic Memory Leak Hunter

Systematically identify and fix memory leaks in a production application using heap snapshots and allocation tracking

prompt template
You are a performance debugging specialist. Help me systematically find and fix a memory leak in my application:

**Symptoms:** [DESCRIBE — e.g., "Memory grows from 200MB to 2GB over 24 hours, then OOM kills the process"]
**Runtime:** [RUNTIME — e.g., "Node.js 20 / Python 3.12 / Java 21 / Go 1.22"]
**Application type:** [TYPE — e.g., "Express API server / React SSR / Background job processor"]
**Already tried:** [WHAT_YOU_TRIED — e.g., "Restarted the process, checked for obvious event listener leaks"]

Guide me through this debugging process:

**Phase 1: Reproduce**
- Exact steps to create a reproducible leak scenario
- How to measure baseline memory (commands and tools)
- Load generation script to accelerate the leak

**Phase 2: Capture**
- How to take heap snapshots at intervals (exact commands)
- How to enable allocation tracking without significant overhead
- What metrics to monitor during the capture window

**Phase 3: Analyze**
- How to compare heap snapshots to find growing objects
- How to identify the retainer chain (what's keeping objects alive)
- Common leak patterns specific to my runtime:
  - [Runtime-specific patterns, e.g., closures over large scopes, event emitter accumulation, timer references, global caches without eviction]

**Phase 4: Fix**
- Code pattern for each identified leak type with before/after examples
- How to verify the fix (expected memory profile shape)
- Preventive patterns to avoid similar leaks in the future

**Phase 5: Monitor**
- Metrics to set up for ongoing leak detection
- Alert thresholds and escalation rules
- Periodic heap snapshot automation for early detection

For each phase, provide exact commands, code snippets, and expected output.

How to Use This Prompt

  1. 1Copy the prompt template above
  2. 2Paste into Claude, ChatGPT, or Cursor
  3. 3Replace [bracketed placeholders] with your specific project details
  4. 4Iterate on the AI output to refine and customize the results