gperftools编译与使用
2016-06-27 22:30
369 查看
参考 http://ju.outofmemory.cn/entry/24487
编译并安装libunwind,命令如下所示。注意这个过程很关键,否者heap profiler产生的数据文件都会是空的。我和小师妹在这个问题上纠结了好久,剑豪也遇到过这个问题。幸好今天听亚夫讲到libunwind,才最终解决了这个问题。
1727 ./configure --prefix=/home/liminghao/tools/libunwind/ CFLAGS=-U_FORTRIFY_SOURCE
1728 make
1729 make install
1753 ./configure --prefix=/home/liminghao/tools/gperftools LDFLAGS=-L/home/liminghao/tools/libunwind/lib CPPFLAGS=-I/home/liminghao/tools/libunwind/include
1754 make
1755 make install
从下面的结果可以看出,foo和bar两个函数分别申请了100MB的内存。
The first column contains the direct memory use in MB.
The fourth column contains memory use by the procedure and all of its callees.
The second and fifth columns are just percentage representations of the numbers in the first and fourth columns.
The third column is a cumulative sum of the second column (i.e., the kth entry in the third column is the sum of the first k entries in the second column.)
编译gperftools
编译并安装libunwind,命令如下所示。注意这个过程很关键,否者heap profiler产生的数据文件都会是空的。我和小师妹在这个问题上纠结了好久,剑豪也遇到过这个问题。幸好今天听亚夫讲到libunwind,才最终解决了这个问题。1727 ./configure --prefix=/home/liminghao/tools/libunwind/ CFLAGS=-U_FORTRIFY_SOURCE
1728 make
1729 make install
1753 ./configure --prefix=/home/liminghao/tools/gperftools LDFLAGS=-L/home/liminghao/tools/libunwind/lib CPPFLAGS=-I/home/liminghao/tools/libunwind/include
1754 make
1755 make install
使用heap profiler
实例代码
#include <iostream> using namespace std; const uint32_t len = 102400; char* foo() { char* p = new char[len]; memset(p, 0, len); return p; } char* bar() { char* p = new char[len]; memset(p, 0, len); return p; } int main(int argc, const char *argv[]) { for (int i = 0; i < 1000; i++) { char* f = foo(); memcpy(f, "abc", 3); char* b = bar(); memcpy(b, "abc", 3); } return 0; }
编译命令
g++ -g google_perf_test.cpp -o google_perf_test
运行命令
$ env LD_PRELOAD="/home/henshao/googleperf_install/lib/libtcmalloc.so" HEAPPROFILE="./perf_log/perf_leak.log" ./google_perf_test Starting tracking the heap Dumping heap profile to ./perf_log/perf_leak.log.0001.heap (100 MB currently in use) Dumping heap profile to ./perf_log/perf_leak.log.0002.heap (Exiting)
分析结果
从下面的结果可以看出,foo和bar两个函数分别申请了100MB的内存。$ pprof --text google_perf_test perf_log/perf_leak.log.0003.heap Using local file google_perf_test. Using local file perf_log/perf_leak.log.0003.heap. Total: 200.0 MB .0 50.0% 50.0% 100.0 50.0% bar .0 50.0% 100.0% 100.0 50.0% foo .0 0.0% 100.0% 0.0 0.0% PrintStats .0 0.0% 100.0% 0.0 0.0% std::string::_Rep::_S_create .0 0.0% 100.0% 0.0 0.0% __static_initialization_and_destruction_0@29a30 .0 0.0% 100.0% 0.0 0.0% 0x00002b71bca8adcf .0 0.0% 100.0% 0.0 0.0% __cxa_finalize .0 0.0% 100.0% 0.0 0.0% __do_global_ctors_aux .0 0.0% 100.0% 0.0 0.0% __do_global_dtors_aux .0 0.0% 100.0% 200.0 100.0% __libc_start_main .0 0.0% 100.0% 0.0 0.0% _fini .0 0.0% 100.0% 0.0 0.0% _init .0 0.0% 100.0% 200.0 100.0% _start .0 0.0% 100.0% 0.0 0.0% exit .0 0.0% 100.0% 200.0 100.0% main .0 0.0% 100.0% 0.0 0.0% std::basic_string::basic_string .0 0.0% 100.0% 0.0 0.0% std::string::_S_copy_chars
小帮助
The first column contains the direct memory use in MB.The fourth column contains memory use by the procedure and all of its callees.
The second and fifth columns are just percentage representations of the numbers in the first and fourth columns.
The third column is a cumulative sum of the second column (i.e., the kth entry in the third column is the sum of the first k entries in the second column.)
相关文章推荐
- 秦始皇的国家道路系统
- 汇编语言 王爽 第二版 实验15
- 归并排序空间复杂度O(1)的实现
- 1002
- 线程池
- 实现360手机助手TabHost的波纹效果
- 36. Valid Sudoku
- stm32f10x.h文件分析理 和 extern "C" { 解释
- day_03_内置函数
- PHP命名空间(Namespace)的使用详解
- POJ 2262 Goldbach's Conjecture(素数)
- 59. Spiral Matrix II
- JavaScript强化教程——JQuery初级教程
- 对Python中文分词模块结巴分词算法过程的理解和分析
- android 图片大小如何设置可以自适应不同分辨率
- 图片拉伸的几种方式
- 配置nginx状态信息虚拟主机
- Swift开发小技巧--自定义Log
- Android 使用Socket实现服务器与手机客户端的长连接四:使用回调函数与Service调用
- springMVC +mybites 中SqlSessionDaoSupport 的使用