1、下载安装包
2、解压安装
# tar -xvf gperftools-2.0.tar.gz
# cd gperftools-2.0
# ./configure
#make
#make install
3、测试
3.1、编写测试用例
# cat main.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4
5 int main()
6 {
7 printf("hello,world\n");
8 char *p = (char*)malloc(1024*1024);
9 //free(p);
10
11 char *buf = (char*)malloc(1024);
12 return 0;
13 }
|
3.2、设置相关的环境变量
# which pprof
/usr/local/bin/pprof
# export PPROF_PATH=/usr/local/bin/pprof
# env HEAPCHECK=normal
LD_PRELOAD="/usr/local/lib/libtcmalloc.so"
./test
WARNING: Perftools heap leak checker is active -- Performance may suffer
hello,world
Leak check _main_ detected leaks of 1049600 bytes in 2 objects
The 2 largest leaks:
Using local file ./test.
Leak of 1048576 bytes in 1 objects allocated from:
@ 80484bd main
######没法定位到行????##############
@ 244e9c __libc_start_main
@ 80483e1 _start
Leak of 1024 bytes in 1 objects allocated from:
@ 80484cc main
######没法定位到行????##############
@ 244e9c __libc_start_main
@ 80483e1 _start
If the preceding stack traces are not enough to find the leaks, try running THIS shell command:
pprof ./test "/tmp/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
|
我们可以使用pprof工具查看内存使用情况
# pprof --text ./test
"/tmp/test.25651._main_-end.heap"
Using local file ./test.
Using local file /tmp/test.25651._main_-end.heap.
Total: 1.0 MB
1.0 100.0% 100.0% 1.0 100.0% main
0.0 0.0% 100.0% 1.0 100.0% __libc_start_main
0.0 0.0% 100.0% 1.0 100.0% _start
|
https://gperftools.googlecode.com/svn/trunk/doc/heapprofile.html#pprof