简体   繁体   中英

Detect memory leak for a running process on Linux

我正在尝试检测进程运行时是否存在内存泄漏-是否可以使用顶级vmstat等来实现。我不想使用valgrind之类的工具,请在此处进行净化。

No. You can't detect memory leak using top or vmstat.

top and vmstat have their own purposes. top helps in monitoring the total number of systems process and their states (whether they're running or waiting).

The memory info top shows, are system wide memory uses info. It won't help you figure out where your app is leaking memory.

vmstat is also the same, but, it provides more info about system memory uses which helps system programmers figure out how memory management layer is working.

If you don't want to use tool programs, You may add some diagnostic inside of your program:

You may use mallinfo() function in your program for checking how much malloc is used.

Or you can use malloc trace by using mtrace() function.

I think test process (embedded memory statistic module) + a remote monitor tool can works.

1) Like Joachim Pileborg and User1 said, an embedded module is need in your code to calculate memory statistics.

2) But for the convenience, a remote tool is need for monitoring, like vmstat/top. And I think this tool should be developed with this embedded module. So and IPC mechanism is need. This's another topic I think.

3) With the above module and tool in hand, you can get info from the test process in soft real time and without and any interrupt of the service.

Memory profiling Use Perf tool to check the leaks.

A sample usage of probes with perf could be to check libc's malloc() and free() calls:

$ perf probe -x /lib64/libc.so.6 malloc

$ perf probe -x /lib64/libc.so.6 free

Added new event: probe_libc:malloc (on 0x7eac0)

A probe has been created. Now, let's record the global usage of malloc and free across all the system during 4 second:

$ perf record -e probe_libc:malloc -agR sleep 4

$ perf record -e probe_libc:free -agR sleep 4

Let's record the usage of malloc and free across all any process during 4 second:

$ perf stat -e probe_libc:free -e probe_libc:malloc -ag -p $(pgrep $process_name$) sleep 4

Output:

Performance counter stats for process id '1153':

11,312 probe_libc:free

11,644 probe_libc:malloc

4.001091828 seconds time elapsed

If there is increase in difference between malloc and free count for every time perf command is run, it is a hint of memory leak.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM