google-perftools工具
Centos7安装
[root@locahost]# cat /etc/centos-release
CentOS Linux release 7.4.1708
#安装unwind
[root@locahost]# wget http://download.savannah.gnu.org/releases/libunwind/libunwind-0.99.tar.gz
[root@locahost]# tar -zxvf libunwind-0.99.tar.gz
[root@locahost]# cd libunwind-0.99
[root@locahost]# ./configure
[root@locahost]# make
[root@locahost]# make install
#卸载系统自带的gperftools-libs
[root@locahost]# rpm -ivh gperftools-libs-2.6.1-1.el7.x86_64.rpm
Preparing... ######################[100%]
file /usr/lib64/libprofiler.so.0 from install of gperftools-libs-2.6.1-1.el7.x86_64 conflicts with from gperftools-libs-2.4.8-1.el7.x86_64
file /usr/lib64/libtcmalloc.so.4 from install of gperftools-libs-2.6.1-1.el7.x86_64 conflicts with from gperftools-libs-2.4.8-1.el7.x86_64
file /usr/lib64/libtcmalloc_and_profiler from install of gperftools-libs-2.6.1-1.el7.x86_64 conflicts with from gperftools-libs-2.4.8-1.el7.x86_64
file /usr/lib64/libtcmalloc_debug.so.4 from install of gperftools-libs-2.6.1-1.el7.x86_64 conflicts with from gperftools-libs-2.4.8-1.el7.x86_64
file /usr/lib64/libtcmalloc_minimal.so.4 from install of gperftools-libs-2.6.1-1.el7.x86_64 conflicts with from gperftools-libs-2.4.8-1.el7.x86_64
file /usr/lib64/libtcmalloc_minimal_debug.so.4 from install of gperftools-libs-2.6.1-1.el7.x86_64 conflicts with from gperftools-libs-2.4.8-1.el7.x86_64
[root@locahost]# rpm -e --nodeps gperftools-libs-2.4.8-1.el7.x86_64
#安装系统自带的gperftools-libs
[root@locahost]# rpm -ivh gperftools-libs-2.6.1-1.el7.x86_64.rpm
[root@locahost]# rpm -ivh gperftools-devel-2.6.1-1.el7.x86_64.rpm
#安装pprof工具
[root@locahost]# rpm -ivh libXaw-1.0.13-4.el7.x86_64.rpm
[root@locahost]# rpm -ivh graphviz-2.30.1-22.el7.x86_64.rpm
[root@locahost]# rpm -ivh pprof-2.6.1-1.el7.noarch.rpm
内存泄漏检测
设置生成heap记录文件条件
root@M800:/home/banting/USB-Device-Mgr/USB-Device-Mgr/bin# export HEAP_PROFILE_TIME_INTERVAL=3
root@M800:/home/banting/USB-Device-Mgr/USB-Device-Mgr/bin# export HEAP_PROFILE_ALLOCATION_INTERVA=104857600
root@M800:/home/banting/USB-Device-Mgr/USB-Device-Mgr/bin# export HEAP_PROFILE_INUSE_INTERVAL=1048576
root@M800:/home/banting/USB-Device-Mgr/USB-Device-Mgr/bin# export HEAPPROFILE=/home/banting/USB-Device-Mgr/USB-Device-Mgr/heap/mybin.hprof
运行程序
root@M800:/home/banting/USB-Device-Mgr/USB-Device-Mgr/bin# export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:../lib_debug/libunwind:../lib_debug/libgerftool:../lib:/usr/local/lib
root@M800:/home/banting/USB-Device-Mgr/USB-Device-Mgr/bin# ./USB-Device-Mgr
[banting@M800 heap]$ ls -tlr
总用量 10888
-rw-r--r-- 1 root root 9390 4月 23 14:21 mybin.hprof.0001.heap
......
-rw-r--r-- 1 root root 158987 4月 23 14:28 mybin.hprof.0083.heap
比较heap记录文件差异
[banting@M800 heap]$ ls -tlr
总用量 10888
-rw-r--r-- 1 root root 9390 4月 23 14:21 mybin.hprof.0001.heap
......
-rw-r--r-- 1 root root 158987 4月 23 14:28 mybin.hprof.0083.heap
[banting@M800 heap]$ pprof --base=mybin.hprof.0001.heap ../bin/USB-Device-Mgr mybin.hprof.0083.heap --show_bytes --lines --heapcheck
Using local file ../bin/USB-Device-Mgr.
Using local file mybin.hprof.0083.heap.
/usr/bin/addr2line: DWARF error: section .debug_info is larger than its filesize! (0xa96d5 vs 0x90be8)
/usr/bin/addr2line: DWARF error: section .debug_info is larger than its filesize! (0x5d4f30 vs 0x4369b8)
Welcome to pprof! For help, type 'help'.
(pprof) top
Total: 894140 B
525312 58.8% 58.8% 526896 58.9% xtcp_socket::xtcp_socket /home/banting/USB-Device-Mgr/xbasicasio.hpp:123
123168 13.8% 72.5% 123456 13.8% mgr_network::on_tcp_svr_msg /home/banting/USB-Device-Mgr/mgr_network.cpp:55
82112 9.2% 81.7% 82304 9.2% mgr_serial::add_device_serial /home/banting/USB-Device-Mgr/serials/mgr_serial.cpp:267
41984 4.7% 86.4% 41984 4.7% scandirat64@@GLIBC_2.15 ??:0
32816 3.7% 90.1% 32816 3.7% opendir@@GLIBC_2.2.5 ??:0
18608 2.1% 92.2% 18608 2.1% boost::detail::heap_new /usr/local/include/boost/thread/pthread/thread_heap_alloc.hpp:24
13104 1.5% 93.6% 13104 1.5% _dl_allocate_tls@@GLIBC_PRIVATE ??:0
13072 1.5% 95.1% 539968 60.4% xtcp_server::create_client /home/banting/USB-Device-Mgr/xbasicasio.hpp:616
10240 1.1% 96.2% 10240 1.1% _IO_file_doallocate@@GLIBC_2.2.5 ??:0
5628 0.6% 96.9% 5628 0.6% boost::asio::detail::thread_info_base::allocate /usr/local/include/boost/asio/detail/thread_info_base.hpp:60
(pprof) quit
[banting@M800 heap]$
查找某个时间节点heap记录文件中内存未释放量
root@M800:/home/banting/USB-Device-Mgr/USB-Device-Mgr/heap# pprof --text ../bin/USB-Device-Mgr mybin.hprof.0009.heap --show_bytes --lines --heapcheck
Using local file ../bin/USB-Device-Mgr.
Using local file mybin.hprof.0009.heap.
/usr/bin/addr2line: DWARF error: section .debug_info is larger than its filesize! (0xa96d5 vs 0x90be8)
/usr/bin/addr2line: DWARF error: section .debug_info is larger than its filesize! (0x5d4f30 vs 0x4369b8)
Total: 716892 B
525312 73.3% 73.3% 526896 73.5% xtcp_socket::xtcp_socket /home/banting/USB-Device-Mgr/xbasicasio.hpp:142
82112 11.5% 84.7% 82304 11.5% mgr_serial::add_device_serial /home/banting/USB-Device-Mgr/serials/mgr_serial.cpp:267
32816 4.6% 89.3% 32816 4.6% opendir@@GLIBC_2.2.5 ??:0
18608 2.6% 91.9% 18608 2.6% boost::detail::heap_new /usr/local/include/boost/thread/pthread/thread_heap_alloc.hpp:24
13104 1.8% 93.7% 13104 1.8% _dl_allocate_tls@@GLIBC_PRIVATE ??:0
11008 1.5% 95.3% 537904 75.0% xtcp_server::create_client /home/banting/USB-Device-Mgr/xbasicasio.hpp:652
6144 0.9% 96.1% 6144 0.9% _IO_file_doallocate@@GLIBC_2.2.5 ??:0
5509 0.8% 96.9% 5509 0.8% boost::asio::detail::thread_info_base::allocate /usr/local/include/boost/asio/detail/thread_info_base.hpp:60
4552 0.6% 97.5% 6432 0.9% xmgr_basic::get_instance /home/banting/USB-Device-Mgr/xbasicmgr.hpp:126
3408 0.5% 98.0% 3408 0.5% __gnu_cxx::new_allocator::allocate /usr/include/c++/4.8.2/ext/new_allocator.h:105
2080 0.3% 98.3% 2080 0.3% on_exit@@GLIBC_2.2.5 ??:0
1859 0.3% 98.6% 1859 0.3% std::string::_Rep::_S_create@@GLIBCXX_3.4 ??:0
参考文献:
google-perftools安装 - 简书 (jianshu.com)
高阶干货|如何用gperftools分析深度学习框架的内存泄漏问题 - 码农教程 (manongjc.com)
Google Performance Tools安装以及使用 - 知乎 (zhihu.com)
使用google-perftools优化nginx在高并发时的性能的教程(完整版)_nginx_脚本之家 (jb51.net)
google-perftools安装方法-datao0907-ChinaUnix博客
google-perf-tools 试用 - 吴镝 - 博客园 (cnblogs.com)
性能测试工具Google perftools(简称gperftool)配置及使用_google perftools使用-CSDN博客
使用google-perftools优化nginx在高并发时的性能的教程(完整版) – 苏demo的别样人生_ (libaocai.com)
google-perftools package : Ubuntu (launchpad.net)
Google Performance Tools (sourceforge.net)
gperftool 检测内存泄漏_heap_profile_time_interval-CSDN博客
使用 gperftools 分析程序内存占用情况_gperftools内存分析-CSDN博客
【gperftools】2——heap profiler - 简书 (jianshu.com)
性能测试工具gperftools使用-内存占用分析 - 流了个火 - 博客园 (cnblogs.com)
Google Heap Profiler (sourceforge.net)
内存泄漏分析的利器——gperftools的Heap Checker-CSDN博客
使用gperftools对程序进行性能分析_51CTO博客_gperftools内存分析
tcmalloc原理剖析(基于gperftools-2.1) - 小龙的博客 | XiaoLong’s Blog (gao-xiao-long.github.io)
GitHub - gperftools/gperftools: Main gperftools repository
GitHub - google/pprof: pprof is a tool for visualization and analysis of profiling data
Index of /releases/libunwind/ (gnu.org)
RPM resource gperftool (rpmfind.net)
RPM resource pprof (rpmfind.net)