Total Pageviews

Tuesday, 30 May 2023

memleax:进程的内存泄漏检测工具

 

memleax 可以检测一个运行中进程的内存泄漏情况,使用 C 语言开发。

memleax 附在一个运行的进程中,hooks 内存分配/自由 ApIs,记录所有内存块,并且实时报告在线超过 5 秒的内存块(可以使用 -e 选项来修改这个时间)。

memleax 非常容易使用,无需重新编译程序,也不用重启目标进程。只需要运行 memleax 来监控目标进程,就可以实时获取内存泄漏的报告,然后停止进程,完成监控。

支持环境

    GNU/Linux, x86_64
    FreeBSD, amd64

检测一个运行的进程:
$ memleax [options] <target-pid>

GitHub 地址:https://github.com/WuBingzheng/memleax。

-------------------------------------------------------------------

debugs memory leak of running process.  

memleax

memleax debugs memory leak of a running process by attaching it, without recompiling or restarting.

status

Because the debugging work depends on CPU architecture and OS heavily, and I test memleax only on several programs, and it is not used widely by now. So there must be bugs.

Some known bugs for debugging multi-thread program, #38 and #39.

Besides, I write a new tool libleak, which works by hooking memory functions by LD_PRELOAD. It's much simpler and has much less impact on performance. So I am not going to improve memleax. Try libleak please.

how it works

memleax debugs memory leak of a running process by attaching it. It hooks the target process's invocation of memory allocation and free, and reports the memory blocks which live long enough as memory leak, in real time. The default expire threshold is 10 seconds, however you should always set it by -e option according to your scenarios.

It is very convenient to use, and suitable for production environment. There is no need to recompile the program or restart the target process. You run memleax to monitor the target process, wait for the real-time memory leak report, and then kill it (e.g. by Ctrl-C) to stop monitoring.

memleax follows new threads, but not forked processes. If you want to debug multiple processes, just run multiple memleax.

install by package

There are DEB and RPM packages for releases.

For Arch Linux users, memleax is available in AUR. Thanks to jelly.

For FreeBSD users, memleax is available in FreeBSD Ports Collection. Thanks to tabrarg.

I tried to submit memleax to Fedora EPEL, but failed. Any help is welcomed.

build from source

The development packages of the following libraries are required:

  • libunwind
  • libelf
  • libdw or libdwarf. libdw is preferred. They are used to read dwarf debug-line information. If you do not have them neither, set --disable-debug_line to configure to disable it. As a result you will not see file name and line number in backtrace.

These packages may have different names in different distributions, such as libelf may names libelf, elfutils-libelf, or libelf1.

NOTE: On FreeBSD 10.3, there are built-in libelf and libdwarf already. However another libelf and libdwarf still can be installed by pkg. memleax works with built-in libelf and pkg libdwarf. So you should install libdwarf by pkg, and must not install libelf by pkg.

After all required libraries are installed, run

$ mkdir build
$ cd build
$ cmake ..
$ make
$ sudo make install

usage

start

To debug a running process, run:

$ memleax [options] <target-pid>

then memleax begins to monitor the target process, and report memory leak in real time.

You should always set expire time by -e options according to your scenarios. For example, if you are debugging an HTTP server with keepalive, and there are connections last for more than 5 minutes, you should set -e 360 to cover it. If your program is expected to free every memory in 1 second, you should set -e 2 to get report in time.

from https://github.com/WuBingzheng/memleax

 

No comments:

Post a Comment