1.下载安装包
https://gperftools.googlecode.com/files/gperftools-2.0.tar.gz
2.安装
#tar zxvf gperftools-2.0.tar.gz
#cd gperftools-2.0
#./configure –prefix=`pwd`/usr
#make
#make install
会安装到当前目录的usr目录下,有bin、include、lib和share
主要用bin/pprof 和lib/libtcmalloc.so.4.1.0
3.想使用heap-check时,
LDFLAGS += -L(gperftools/usr/lib) –ltcmalloc
例如在parse_json的test中使用:
(1) 修改Makefile,在pc版的LDFLAGS后加上-L(gperftools_path)/usr/lib –ltcmalloc
(2) export LD_LIBRARY_PATH=/home/ott_release/dynamic_pc/lib //由于要编译pc版的parse_json_test
(3) ./auto-build-pc.sh //生成parse_json_test
(4) export PPROF_PATH=(gperftools_path)/usr/bin //安装时的路径
(5) env HEAPCHECK=normal LD_PRELOAD=(gperftools_path)/usr/lib/libtcmalloc.so.4.1.0 ./parse_json_test
(6) 运行之后会有这样的log
pprof ./parse_json_test "/tmp/parse_json_test.25651._main_-end.heap" --inuse_objects --lines --heapcheck --edgefraction=1e-10 --nodefraction=1e-10 --gv
If you are still puzzled about why the leaks are there, try rerunning this program with HEAP_CHECK_TEST_POINTER_ALIGNMENT=1 and/or with HEAP_CHECK_MAX_POINTER_OFFSET=-1
If the leak report occurs in a small fraction of runs, try running with TCMALLOC_MAX_FREE_QUEUE_SIZE of few hundred MB or with TCMALLOC_RECLAIM_MEMORY=false, it might help find leaks more repeatably
Exiting with error code (instead of crashing) because of whole-program memory leaks
(7) Gperftools_path/usr/bin/pprof --text ./parse_json_test "/tmp/parse_json_test.25651._main_-end.heap"