gperftools使用

本文介绍了gperftools的heap checker和heap profiler在C++内存泄露检测中的应用。heap checker通过不同检查模式定位内存泄露,包括minimal、normal、strict、draconian和local模式。heap profiler则持续监控内存分配,生成profile文件,结合pprof工具分析。使用时可以通过LD_PRELOAD或静态链接方式,并通过环境变量调整检查粒度和范围。
摘要由CSDN通过智能技术生成

近期尝试使用gperftools对C++内存泄露进行排查,所以对gperftools进行了一些了解。gperftools是google用来进行程序性能优化的工具集合,主要包括tcmalloc和profiler两个库。其可以提供如下的功能:

  1. tcmalloc:与glibc中的malloc函数相比,tcmalloc的内存分配效率要远高于malloc,可以提高高并发的性能 ,降低系统的负载
  2. cpu profiler:对程序中各个函数使用的CPU情况进行监控探测
  3. heap checker:对程序是否发生内存泄露进行检测
  4. heap profiler:对程序中申请的内存区域进行监控

heap checker

使用

对于使用heap checker的使用,需要注意的是一般是对整个程序进行检测,要求这个程序会自动退出,而不是那种会不停的运行的服务。其实gperftools的官方文档中说当发现内存泄露的同时会通过exit()使程序退出并打印相关的内存信息。

In this case, the heap-checker starts tracking memory allocations before the start of main(), and checks again at program-exit. If it finds any memory leaks – that is, any memory not pointed to by objects that are still “live” at program-exit – it aborts the program (via exit(1)) and prints a message describing how to track down the memory leak (using pprof).

对于heap checker的使用有两种方法:

  1. 设置LD_PRELOAD 来预加载tcmalloc库,然后执行相关的运行命令,例如:

        LD_PRELOAD=/usr/lib64/libtcmalloc.so
        HEAPCHECK=normal ./example1
    
  2. 在编译时使用静态库进行连接,然后执行相关的运行命令,例如:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值