Memory Leak Playbook
Gradual memory growth leading to OOM kills and service restarts. The slow-motion disaster that starts as a minor curiosity and ends with services falling over one by one. The challenge: finding the leak while keeping the lights on.
MEMORY LEAK INDICATORS
- • RSS memory growing steadily over hours/days
- • GC frequency and duration increasing
- • OOM kills in container/process logs
- • Service restarts becoming more frequent
- • Response times degrading as memory fills
- • Swap usage increasing on host
Immediate Response: Stabilize First
Assess current memory state
How close to OOM? How many instances affected? Is auto-scaling compensating?
Restart the most critical instances
Rolling restart to free memory. This buys time but doesn't fix the leak.
Increase memory limits temporarily
If running in containers, bump memory limits to extend time before next OOM.
Check for recent code changes
Memory leaks often introduced by recent deploys. Check what changed.
Enable memory profiling
Start heap profiling on one instance. Capture heap dumps before and after traffic.
Common Memory Leak Patterns by Language
Node.js / JavaScript
Common Causes
- • Event listeners not cleaned up
- • Closures holding references to large objects
- • Global variables accumulating data
- • Unbounded caches without eviction
- • Circular references preventing GC
- • Streams not properly closed/destroyed
Diagnostic Tools
- • --inspect flag + Chrome DevTools
- • heapdump module for snapshots
- • process.memoryUsage() trending
- • clinic.js / 0x for profiling
Java / JVM
Common Causes
- • Static collections growing unbounded
- • Database connection pool leaks
- • Thread-local variables not cleaned
- • ClassLoader leaks
- • Improper cache implementations
- • Unclosed resources (streams, connections)
Diagnostic Tools
- • jmap for heap dumps
- • Eclipse MAT for analysis
- • jvisualvm / JFR
- • GC logging analysis
Go
Common Causes
- • Goroutine leaks (channel reads never complete)
- • Slice capacity not released
- • Global maps growing unbounded
- • HTTP response bodies not closed
- • Timer/ticker resources not stopped
Diagnostic Tools
- • pprof heap profiling
- • runtime.ReadMemStats()
- • goroutine dump analysis
- • go tool trace
Python
Common Causes
- • Circular references with __del__ methods
- • C extension objects not freed
- • Global lists/dicts accumulating data
- • Django/Flask request context leaks
- • Large objects in module-level scope
Diagnostic Tools
- • tracemalloc module
- • objgraph for reference graphs
- • memory_profiler
- • gc.get_objects() analysis
Short-Term Mitigation Strategy
BUYING TIME
Finding and fixing a memory leak can take days. These strategies keep your service running while you investigate.
Scheduled Restarts
Set up periodic rolling restarts (every 4-8 hours) to reset memory. Not a fix, but prevents OOM. Use graceful shutdown with connection draining.
Increased Instance Count
Run more instances with lower traffic each. Individual instances leak slower with less traffic. Auto-scaling with memory-based triggers.
Memory Limit Guards
Set container memory limits with restart policies. Let the orchestrator kill and restart leaking instances automatically. Ensure health checks detect degradation.
Investigation Methodology
Establish baseline
What's normal memory usage? Chart memory over the past 7 days. When did the leak start?
Correlate with changes
Overlay deploy timeline. Did memory growth pattern change after a specific deployment?
Capture heap snapshots
Take snapshots at startup, after 1 hour, after 4 hours. Compare object counts and sizes.
Identify growing objects
Which object types are accumulating? What's holding references to them? Follow the retention chain.
Reproduce in staging
Can you reproduce with load testing? Use production traffic replay if available.
Fix and validate
Deploy fix. Monitor memory over 24-48 hours. Confirm growth pattern normalized.
Post-Incident Questions
How long was the memory leak active before we detected it?
Why didn't our monitoring catch the gradual increase earlier?
What code change introduced the leak? Was it covered by tests?
Can we add memory regression tests to our CI/CD pipeline?
Are there other services with similar patterns we should check?
Should we implement automatic memory-based rolling restarts as a safety net?