google的内存泄露检测工具,使用libtcmalloc.so代替默认的libc库,对malloc和free、mmap和munmap等内存申请释放的位置进行跟踪。
1、参考说明
2、下载heap checker
yum install gperftools
yum install gperftools-devel.x86_64
3、PRELOAD方式指定替换的so库
在原来的程序启动命令之前加
env LD_PRELOAD="/usr/lib64/libtcmalloc.so" HEAPCHECK=normal
4、测试完成后,程序退出
会在/tmp下生成二进制名称名称的heap文件,程序运行过程中fork出的子进程也都会被跟踪生成heap文件。比如使用system()调用的其他二进制程序,也会生成相应名称的heap文件。
/tmp/arping.18625._main_-end.heap
/tmp/bash.23888._main_-end.heap
/tmp/bash.4498._main_-end.heap
/tmp/bblk_devctl.21685._main_-end.heap
/tmp/bblk_devctl.31927._main_-end.heap
/tmp/arping.26181._main_-end.heap
/tmp/bash.24197._main_-end.heap
/tmp/bash.4892._main_-end.heap
/tmp/bblk_devctl.21768._main_-end.heap
/tmp/bblk_devctl.32013._main_-end.heap
(实际测试发现,如果退出过程出现SIGFAULT,就没有能够成功生成heap文件,直接kill掉是不是也不可以生成?)
5、解析heap文件
pprof命令用来解析heap文件,输入二进制源文件和heap文件,指定输出格式text/pdf。
pprof /taihang/bin/sdpcie /tmp/sdpcie.4548._main_-end.heap --text
pdf文件是生成了文件泄露的执行路径,但好像只有泄露最多的那个路径。
text会直接以列表的形式显示到stdout。
/home/test/dev_op # pprof /taihang/bin/sdpcie /tmp/sdpcie.4548._main_-end.heap --text
Using local file /taihang/bin/sdpcie.
Using local file /tmp/sdpcie.4548._main_-end.heap.
Total: 0.0 MB
0.0 99.7% 99.7% 0.0 99.7% tlp_inflight_ring_create
0.0 0.3% 100.0% 0.0 0.3% std::string::_Rep::_S_create
0.0 0.0% 100.0% 0.0 0.0% __gnu_cxx::new_allocator::allocate (inline)
0.0 0.0% 100.0% 0.0 99.7% __clone
0.0 0.0% 100.0% 0.0 0.3% brpc::GlobalUpdate
0.0 0.0% 100.0% 0.0 0.0% brpc::SocketMap::List
0.0 0.0% 100.0% 0.0 0.3% bthread::TaskGroup::task_runner
0.0 0.0% 100.0% 0.0 0.3% bthread_make_fcontext
0.0 0.0% 100.0% 0.0 0.3% butil::FileWatcher::init_from_not_exist
0.0 0.0% 100.0% 0.0 99.7% elpf_init
0.0 0.0% 100.0% 0.0 99.7% elpf_pcisw_main
0.0 0.0% 100.0% 0.0 99.7% elpf_task_init
0.0 0.0% 100.0% 0.0 0.0% emplace_back (inline)
0.0 0.0% 100.0% 0.0 99.7% pcisw_main
0.0 0.0% 100.0% 0.0 99.7% pcisw_main_init
0.0 0.0% 100.0% 0.0 99.7% sdpcie_bypass_channel_init
0.0 0.0% 100.0% 0.0 99.7% start_thread
0.0 0.0% 100.0% 0.0 0.0% std::_Vector_base::_M_allocate (inline)
0.0 0.0% 100.0% 0.0 0.3% std::string::_M_mutate
0.0 0.0% 100.0% 0.0 0.3% std::string::_M_replace_safe
0.0 0.0% 100.0% 0.0 0.3% std::string::assign (inline)
0.0 0.0% 100.0% 0.0 0.3% std::string::operator= (inline)
0.0 0.0% 100.0% 0.0 0.0% std::vector::_M_emplace_back_aux
0.0 0.0% 100.0% 0.0 0.0% std::vector::push_back (inline)
0.0 0.0% 100.0% 0.0 99.7% tlp_ring_init