gperftools -- heapcheck 使用

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 }
     
    # gcc -g -o test main.c -ltcmalloc    # -g 编译选项可以忽略

    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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值