How can I find a memory leak of a running process?
Solution 1
Here are the steps that almost guarantee to find what is leaking memory:
-
Find out the PID of the process which causing memory leak.
ps -aux
capture the
/proc/PID/smaps
and save into some file likeBeforeMemInc.txt
.- wait till memory gets increased.
- capture again
/proc/PID/smaps
and save it hasafterMemInc.txt
-
find the difference between first
smaps
and 2ndsmaps
, e. g. withdiff -u beforeMemInc.txt afterMemInc.txt
-
note down the address range where memory got increased, for example:
beforeMemInc.txt afterMemInc.txt --------------------------------------------------- 2b3289290000-2b3289343000 2b3289290000-2b3289343000 #ADDRESS Shared_Clean: 0 kB Shared_Clean: 0 kB Shared_Dirty: 0 kB Shared_Dirty: 0 kB Private_Clean: 0 kB Private_Clean: 0 kB Private_Dirty: 28 kB Private_Dirty: 36 kB Referenced: 28 kB Referenced: 36 kB Anonymous: 28 kB Anonymous: 36 kB #INCREASE MEM AnonHugePages: 0 kB AnonHugePages: 0 kB Swap: 0 kB Swap: 0 kB KernelPageSize: 4 kB KernelPageSize: 4 kB MMUPageSize: 4 kB MMUPageSize: 4 kB Locked: 0 kB Locked: 0 kB VmFlags: rd wr mr mw me ac VmFlags: rd wr mr mw me ac
use GDB to dump memory on running process or get the coredump using
gcore -o process
-
I used gdb on running process to dump the memory to some file.
gdb -p PID dump memory ./dump_outputfile.dump 0x2b3289290000 0x2b3289343000
-
now, use
strings
command orhexdump -C
to print thedump_outputfile.dump
strings outputfile.dump
You get readable form where you can locate those strings into your source code.
Analyze your source to find the leak.
Solution 2
I think memleax is exact what you want.
It debugs memory leak of a running process by attaching it, without recompiling program or restarting target process. It's very convenient and suitable for production environment.
It works on GNU/Linux and FreeBSD.
NOTE: I'm the author, any suggestion is welcome.
== EDIT ==
I also wrote another tool libleak, which hooks memory functions by LD_PRELOAD.
There is no need to modify the target program. Although you have to restart the progress with LD_PRELOAD, you can enable/disable the detection during running.
There is much less impact on performance since no signal trap.
Compared with similar tools (such as mtrace), it print the full call-stack at suspicious memory leak point.
Solution 3
On Linux, you could enable mtrace in your program, but it is a code change.
On OpenBSD, you could try the malloc stats.
Google's leak checker might also be worth a look, and unlike mtrace you may be able to use LD_PRELOAD
to avoid recompilation.
Related videos on Youtube
Enuma Elish
Updated on September 18, 2022Comments
-
Enuma Elish almost 2 years
Is there a way, I can find the memory leak of a running process? I can use Valgrind for finding memory leaks before the start of a process. I can use GDB to attach it to a running process. How could I debug a memory leaks of a running process?
-
user400344 almost 8 yearsValgrind is very useful, I would even call it intuitive.
-
-
stiv almost 4 yearsstrings command doesn't give me any hint where to look at my source files. for me i just see some garbage strings.