How to find memory leaks of a process?

2019-07-29 04:57发布

I need to find out the memory usage of a particular process. In fact I need to find out there is any memory leak in the application I have written. I cannot use memfree or /proc/meminfo since our system has log folder mounted in the RAM.

I have gone through lot of similar queries, and some have suggested to use ps aux command. I’m kinda confused on which parameter gives the correct memory usage or maybe memory leak after few hours. PS AUX gives VSZ(virtual mem) and RSS(resident set size).

I have written a sample program which allocates 4 bytes of memory and De-allocates it. After running the program, it seems VSZ value increases when memory is allocated but not decreased when De-allocated. But RSS value showed correct, increases when allocated and decreased when De-allocated.

Can anybody confirm whether using RSSvalue will point to the amount of memory leak in the code? Or is there any other method?

3条回答
淡お忘
2楼-- · 2019-07-29 05:31

I know this is ancient, but I feel the need to say that for something like this, you really just want to use a tool like Valgrind. Namely, Valgrind. That's definitely the way to go, especially with a program you're writing (or have written), since you can tweak the compile flags as well so you can get the most useful output. Assuming you're using gcc, try compiling with -g to turn on debug symbols and don't strip the binary.

Usage is pretty simple, and the docs are on the linked website. Basic usage is just valgrind program on the command line. It'll show you not only specifics, but a nice summary of memory leaked at the end.

查看更多
萌系小妹纸
3楼-- · 2019-07-29 05:38

To know the details you can use pmap: pmap pid

root@tm# pmap 1216
1216:   /usr/sbin/acpid
08048000     32K r-x--  /usr/sbin/acpid
08050000      4K rw---  /usr/sbin/acpid
08051000      4K rw---    [ anon ]
088f2000    140K rw---    [ anon ]
b7642000      4K rw---    [ anon ]
b7643000   1280K r-x--  /lib/i686/cmov/libc-2.11.3.so
b7783000      4K -----  /lib/i686/cmov/libc-2.11.3.so
b7784000      8K r----  /lib/i686/cmov/libc-2.11.3.so
b7786000      4K rw---  /lib/i686/cmov/libc-2.11.3.so
b7787000     12K rw---    [ anon ]
b7798000      8K rw---    [ anon ]
b779a000      4K r-x--    [ anon ]
b779b000    108K r-x--  /lib/ld-2.11.3.so
b77b6000      4K r----  /lib/ld-2.11.3.so
b77b7000      4K rw---  /lib/ld-2.11.3.so
bfd59000     84K rw---    [ stack ]
 total     1704K
查看更多
Lonely孤独者°
4楼-- · 2019-07-29 05:39

I use top for this sort of thing.

top -p <process id>
查看更多
登录 后发表回答