Find and fix memory leaks by analyzing code patterns, symptoms, and environment for common leak sources.
Fill in this template
The {{ }} placeholders are variables — drop in your own details, then copy the ready-to-use prompt. (Or copy the template as-is and let your AI assistant fill them in.)
0/3 filled
Preview filled prompt
You are a performance engineer specializing in memory management and leak detection.
## Code Under Investigation
```
{{CODE}}
```
## Symptoms
{{SYMPTOMS}}
## Environment
{{ENVIRONMENT}}
## Memory Leak Investigation
### Step 1 — Symptom Analysis
- What memory growth pattern is observed? (linear, exponential, step-wise)
- When does it start? (immediately, after N operations, after specific actions)
- Does it recover after garbage collection, or is it persistent?
- What is the rate of growth? (MB/hour, MB/request)
### Step 2 — Common Leak Patterns
Inspect the code for these common sources:
#### Event Listeners & Subscriptions
- Are addEventListener calls paired with removeEventListener?
- Are RxJS/Observable subscriptions unsubscribed in cleanup?
- Are WebSocket or SSE handlers cleaned up on disconnect?
- Are React useEffect hooks returning cleanup functions?
#### Timers & Intervals
- Are setInterval calls cleared with clearInterval?
- Are setTimeout calls cleared when components unmount?
- Are requestAnimationFrame calls cancelled?
#### Closures & References
- Do closures capture references to large objects that outlive their usefulness?
- Are callbacks holding references to DOM nodes that have been removed?
- Are WeakRef or WeakMap used where appropriate?
#### Caches & Collections
- Do Maps, Sets, or arrays grow without bounds?
- Is there a cache eviction policy (LRU, TTL, max size)?
- Are cache keys accumulating stale entries?
#### DOM-Specific
- Are detached DOM nodes still referenced in JavaScript?
- Are MutationObservers or IntersectionObservers disconnected?
- Are global references to component instances accumulating?
#### Server-Specific
- Are database connections returned to the pool?
- Are streams properly closed (readable.destroy(), writable.end())?
- Are request-scoped resources cleaned up after response?
### Step 3 — Diagnostic Approach
Recommend profiling steps:
- Which heap snapshot comparisons to take
- What allocation timeline analysis to perform
- What specific metrics to monitor
## Output Format
1. **Leak Classification**: Type of leak and estimated severity
2. **Identified Leaks**: Each leak source with the specific code line
3. **Fix for Each Leak**: Corrected code with proper cleanup
4. **Prevention Patterns**: Reusable patterns to avoid this class of leak
5. **Monitoring**: What to monitor to detect recurrence (metrics, alerts)Tags
debuggingmemory-leakperformanceprofiling
Explore more prompts and rules
BeforeMerge offers hundreds of AI prompts, code review rules, guides, and detection patterns to help your team ship better code.