- Memory usage increasing continuously over time
Diagnose and fix memory leaks in Java, Node.js, and Python applications. Capture heap snapshots, identify unbounded caches and unclosed connections, and apply immediate mitigations like restarts or LRU caches.
/plugin marketplace add anton-abyzov/specweave/plugin install sw-infra@specweave# Monitor memory over time (5 minute intervals)
watch -n 300 'ps aux | grep <process> | awk "{print \$4, \$5, \$6}"'
# Check if memory continuously increasing
# Leak: 20% → 30% → 40% → 50% (linear growth)
# Normal: 30% → 32% → 31% → 30% (stable)
Java (Heap Dump):
# Get heap dump
jmap -dump:format=b,file=heap.bin <PID>
# Analyze with jhat or VisualVM
jhat heap.bin
# Open http://localhost:7000
# Or use Eclipse Memory Analyzer
Node.js (Heap Snapshot):
# Start with --inspect
node --inspect index.js
# Chrome DevTools → Memory → Take heap snapshot
# Or use heapdump module
const heapdump = require('heapdump');
heapdump.writeSnapshot('/tmp/heap-' + Date.now() + '.heapsnapshot');
Python (Memory Profiler):
# Install memory_profiler
pip install memory_profiler
# Profile function
python -m memory_profiler script.py
Look for:
Common patterns:
// 1. Global cache growing forever
global.cache = {}; // Never cleared
// 2. Event listeners not removed
emitter.on('event', handler); // Never removed
// 3. Timers not cleared
setInterval(() => { /* ... */ }, 1000); // Never cleared
// 4. Closures
function createHandler() {
const largeData = new Array(1000000);
return () => {
// Closure keeps largeData in memory
};
}
Option A: Restart Application
# Restart to free memory
systemctl restart application
# Impact: Memory usage returns to baseline
# Risk: Low (brief downtime)
# NOTE: This is temporary, leak will recur!
Option B: Increase Memory Limit (temporary)
# Java
java -Xmx4G -jar application.jar # Was 2G
# Node.js
node --max-old-space-size=4096 index.js # Was 2048
# Impact: Buys time to find root cause
# Risk: Low (but doesn't fix leak)
Option C: Scale Horizontally (cloud)
# Add more instances
# Use load balancer to rotate traffic
# Restart instances on schedule (e.g., every 6 hours)
# Impact: Distributes load, restarts prevent OOM
# Risk: Low (but doesn't fix root cause)
Analyze heap dump and identify leak source
Common Fixes:
1. Add LRU Cache
// BAD: Unbounded cache
const cache = {};
// GOOD: LRU cache with size limit
const LRU = require('lru-cache');
const cache = new LRU({ max: 1000 });
2. Remove Event Listeners
// Add listener
const handler = () => { /* ... */ };
emitter.on('event', handler);
// CRITICAL: Remove later
emitter.off('event', handler);
// React/Vue: cleanup in componentWillUnmount/onUnmounted
3. Clear Timers
// Set timer
const intervalId = setInterval(() => { /* ... */ }, 1000);
// CRITICAL: Clear later
clearInterval(intervalId);
// React: cleanup in useEffect return
useEffect(() => {
const id = setInterval(() => { /* ... */ }, 1000);
return () => clearInterval(id);
}, []);
4. Close Connections
// BAD: Connection leak
const conn = await db.connect();
await conn.query(/* ... */);
// Connection never closed!
// GOOD: Always close
const conn = await db.connect();
try {
await conn.query(/* ... */);
} finally {
await conn.close(); // CRITICAL
}
Escalate to developer if:
Escalate to platform team if:
finally)After resolving:
Designs feature architectures by analyzing existing codebase patterns and conventions, then providing comprehensive implementation blueprints with specific files to create/modify, component designs, data flows, and build sequences