美文网首页
TCMalloc源码阅读

TCMalloc源码阅读

作者: zcyzcy | 来源:发表于2022-02-25 10:28 被阅读0次

    安装gperftools

    // 1. 下载gperftools代码
    git clone https://github.com/gperftools/gperftools.git
    
    // 2. 执行./autogen.sh
    $ ./autogen.sh
    
    // 3. 执行./configure
    $ ./configure CXX=/opt/compiler/gcc-8.2/bin/g++ CC=/opt/compiler/gcc-8.2/bin/gcc
    
    // 4.更新环境变量
    export CPPFLAGS='-l\usr\local\include'$CPPFLAGS
    export LDFLAGS='-L\usr\local\lib'$LDFAGS
    
    // 5. 编译并安装
    $ make
    $ sudo make install 
    $ sudo ldconfig
    

    写代码测试内存泄漏

    #include <iostream>
    int func() {
      int *p = new int(10);
      return 0;
    }
    
    int main() {
      std::cout << "memory leak test" << std::endl;
      return func();
    }
    

    设置PPROF_PATH环境变量,否则无法打印调用栈的符号信息。

    export PPROF_PATH=/usr/local/bin/pprof
    

    链接tcmalloc库,编译

    /opt/compiler/gcc-8.2/bin/g++ -std=c++0x -g -o memory_leak memory_leak.cpp -ltcmalloc -L/usr/local/lib
    

    如果运行中出现找不到运行时链接库,需要增加运行时库文件搜索路径


    image.png
    LD_LIBRARY_PATH=/usr/local/lib
    echo $LD_LIBRARY_PATH
    
    成功运行: image.png

    相关文章

      网友评论

          本文标题:TCMalloc源码阅读

          本文链接:https://www.haomeiwen.com/subject/lzxalrtx.html