How do you diagnose a memory leak in a long-running process?
AdvancedProductionPerformance & the USE Method
Answer
- Confirm growth over time:
pidstat -r -p <pid> 60or RSS from monitoring; a sawtooth that returns to baseline is GC, a steady ramp is a leak. - Check whether it is heap (
AnonPages,smaps_rollupAnonymous), file mappings, or kernel memory (slabtopforSUnreclaim). - Use language tools: Java heap dumps (
jcmd <pid> GC.heap_dump), Gopprof, Pythontracemalloc, nativevalgrind/heaptrack, ormemleak-bpfcc -p <pid>. - Mitigate while fixing with
MemoryMax=and restarts, so the leak cannot take down the host.
More on Performance & the USE Method
What is the USE method?BeginnerMap the USE method to concrete Linux commands.IntermediateWhat is your "first 60 seconds" checklist on a slow server?IntermediateHow do you read vmstat output?IntermediateHow do you read `iostat -xz 1`?IntermediateWhat is sar and why install sysstat on every server?Beginner