概述
gperftools是一个高性能多线程内存管理实现的集合(TCMalloc),用于替代系统的内存分配函数(malloc, free, new, new[], delete, delete[]) 外加一些性能分析工具(heap-checker, heap-profiler, cpu-profiler)
安装
详细参考INSTALL 0. 获取源码
// curl也可以用于下载
wget https://github.com/gperftools/gperftools/releases/download/gperftools-2.8.1/gperftools-2.8.1.tar.gz
- 安装依赖
autoconf automake libtool
// 可以选择源码安装或者yum install
// 源码安装的三个步骤: 1.配置(configure)2..编译(make)3.安装(make install)
// ./configure --help 输出配置选项列表, 其中--prefix配置安装的路径
// 不配置该选项, 安装后的路径比较杂乱
// 可执行文件默认路径/usr/local/bin
// 库文件默认路径/usr/local/bin
// 配置文件默认路径/usr/local/etc
// 其它资源文件在/usr/local/share
// --prefix的好处是移植或者删除软件, 只需要删除指定的安装目录就可以
// 源码安装autoconf[镜像](http://mirrors.kernel.org/gnu/autoconf/)
wget http://mirrors.kernel.org/gnu/autoconf/autoconf-2.69.tar.gz && tar -xzvf autoconf-2.69.tar.gz
cd autoconf-2.69
./configure --prefix=PATH
make
make install
// 源码安装automake[镜像](http://mirrors.kernel.org/gnu/automake/)
wget http://mirrors.kernel.org/gnu/automake/automake-1.16.tar.gz && tar -xzvf automake-1.16.tar.gz
cd automake-1.16
./configure --prefix=PATH
make
make install
// 源码安装libtool[镜像](http://mirrors.kernel.org/gnu/libtool/)
wget http://mirrors.kernel.org/gnu/libtool/libtool-2.4.6.tar.gz && tar -xzvf libtool-2.4.6.tar.gz
cd libtool-2.4.6
./configure --prefix=PATH
make
make install
- 生成configure文件
./autogen.sh
- 生成Makefile
./configure
- 编译&&安装
make && make install
tcmalloc
动态库的方式,只需要链接
-ltcmalloc或-ltcmalloc_minimal即可获得tcmalloc的优点: 替代malloc和new
这里7.6日之前有一句NOTE, 后边被一次提交去掉了:
用gcc编译程序时, 打算链接libtcmalloc, 最安全的做法是传入下列标志:
-fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free
编译时, gcc会产生一些优化, 假设使用它内置的内存管理函数; 使用tcmalloc时这种假设是不正确的. 实际上, 这种情况下还没有发现任何问题
heap profiler
- 动态库的方式,使用
-ltcmalloc链接可执行程序- 在设置了如下环境变量的情况下执行二进制程序:
$ HEAPPROFILE=/tmp/heapprof <path/to/binary> [binary args]
- 运行pprof分析heap使用情况
$ pprof <path/to/binary> /tmp/heapprof.0045.heap # run 'ls' to see options
$ pprof --gv <path/to/binary> /tmp/heapprof.0045.heap
heap checker
为了捕获所有的堆泄漏, 必须将tcmalloc链接到最后. heap checker可能会误报在tcmalloc之后链接的库的存在内存泄漏, 实际上这些库并没有
heap checker目前只支持linux
- 使用
-ltmalloc链接可执行程序- 在设置了如下环境变量的情况下执行二进制程序:
// HEAPCHECK的其它值: normal(和"1"相同), strict, draconian
$ HEAPCHECK=1 <path/to/binary> [binary args]
cpu profiler
cpu profiler在所有的基于unix系统上有效, 但是目前不支持Win
cpu profiling在fork子进程后不起作用
- 使用
-lprofiler链接可执行程序- 在设置了如下环境变量后执行二进制程序:
$ CPUPROFILE=/tmp/prof.out <path/to/binary> [binary args]
- 运行pprof分析CPU的使用
$ pprof <path/to/binary> /tmp/prof.out # -pg-like text output
$ pprof --gv <path/to/binary> /tmp/prof.out # really cool graphical output
总结
如果要使用 cpu profiler, heap profiler, heap leak-checker, 可以如下操作:
gcc -o myapp ... -lprofiler -ltcmalloc
如果用静态库版本, 下面两个库不起作用:
gcc -o myapp ... /usr/lib/libprofiler.a /usr/lib/libtcmalloc.a # errors!
应该使用
libtcmalloc_and_profiler.a达到目的:
gcc -o myapp ... /usr/lib/libtcmalloc_and_profiler.a
如果只想使用最基础内存分配库,
libtcmalloc_minimal.a可以完美的作为malloc/new的替代
64-bit issues
64位Linux环境下, gperftools使用glibc内置的stack-unwinder可能会引起死锁, 官方推荐配置和安装gperftools之前先安装libunwind-0.99-beta, 版本太新或太旧都可能有问题
即使使用libunwind,64位系统上也有可能有问题, 但只影响
heap-checker, heap-profiler, cpu-profiler, TCMalloc不受影响
x86-64下, 如果不希望安装libunwind, 也可以使用gperftools内置的stack unwinder, 在编译gperftools时, 执行
./configure --enable-frame-pointers指定使用内置的stack unwinder, 应用程序指定-fno-omit-frame-pointer编译选项