本文主要介绍 gperftools 内存分析工具中的 tcmalloc最小交叉编译及其使用的方法。
1.下载, 我这里使用的是最新版本 gperftools-2.9.1.tar.gz
https://github.com/gperftools/gperftools/releases/tag/gperftools-2.9.1
2.下载后拷贝至虚拟机linux解压,我是用的版本是 ubuntu 16.04 LTS
tar -xf gperftools-2.9.1.tar.gz
cd gperftools-2.9.1
导入交叉工具链环境变量,根据实际情况修改:
export PATH=/code/toolchain/gcc-arm-8.2-2018.08-x86_64-arm-linux-gnueabihf/bin:${PATH}
查看配置选项:
./configure --help
我这里只需要 TCMALLOC 我的配置选项如下:
./configure \
--disable-cpu-profiler --disable-heap-profiler \
--disable-heap-checker \
--disable-debugalloc --disable-libtool-lock \
--disable-deprecated-pprof \
--enable-minimal \
--prefix=/code/gperftools-2.9.1/arm_install \
--host=arm-linux-gnueabihf
其中 --prefix=/code/gperftools-2.9.1/arm_install 为你的安装路径
--host=arm-linux-gnueabihf 指定你的交叉工具链前缀
编译:
make -j8
make install
正常情况下编译成功后会在 安装路径 生成对应的库
在代码中使用:
test.cpp
#include <stdio.h>
#include <stdlib.h>
int main() {
printf("start\n");
int * ss = new int;
*ss = 0;
delete ss;
int * s2 = (int * )malloc(sizeof(int));
*s2 = 123;
free(s2);
printf("end\n");
return 0;
}
和标准库使用一样,只是编译的时候需链接库:
arm-linux-gnueabihf-g++ test.cpp -o test -L./arm_install/lib -ltcmalloc_minimal
把生成的 test 和 整个lib目录拷贝到目标系统中指定lib库目录即可运行:
export LD_LIBRARY_PATH=/xxxx/lib:$LD_LIBRARY_PATH
./test