默认编译jemalloc没开启prof功能,要自己开启后编译。

  1. wget https://github.com/jemalloc/jemalloc/archive/5.1.0.tar.gz
  2. tar zxvf 5.1.0.tar.gz
  3. cd jemalloc-5.1.0/
  4. ./autogen.sh
  5. ./configure --prefix=/usr/local/jemalloc-5.1.0 --enable-prof
  6. make -j
  7. make install

设置环境变量

  1. "MALLOC_CONF": "prof:true,lg_prof_interval:32,prof_prefix:/home/admin/logs/jeprof.out",
  2. "LD_PRELOAD": "/tmp/libjemalloc.so.2"

安装依赖

  1. sudo yum install ghostscript graphviz jemalloc-devel -y

在节点的logs目录中可以看到jeprof的输出:
image.png
有了jeprof文件,我们可以运行jeprof命令来查看两次snapshot之间的diff:

  1. # 安装依赖(只需一次)
  2. sudo yum install ghostscript graphviz jemalloc-devel -y
  3. jeprof --pdf --base=jeprof.out.322606.197.i197.heap /usr/local/python3/bin/python3.7 jeprof.out.322606.221.i221.heap > 37066_197_221.pdf

然后分析单次snapshot的内存消耗

  1. jeprof --pdf /usr/local/python3/bin/python3.7 jeprof.out.322606.221.i221.heap > 322606_221.pdf

Reference

  1. GitHub: jemalloc
  2. jemalloc use case: Leak Checking
  3. https://jemalloc.net/
  4. 使用jeprof定位内存泄漏