linux cpu占用率如何看
747
2022-10-01
如何用tcmalloc 分析内存泄露
背景
有个大的项目中开发阶段有时候难免会出现内存泄露,很多情况下泄露的地方还很隐蔽,那是否有高效的方法去定位呢?
总体思路
通常我们可以使用 代码review、tcmalloc、assan 等工具进行分析。下面主要介绍使用tcmalloc的方法。
步骤
1. 重新编译二进制
add_compile_options(-fpermissive -mssse3 -g -O0 -fno-omit-frame-pointer -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free)
在动态库后面加上tcmalloc和profiler:target_link_libraries(block_common common thread_pool protobuf proto_bbb brpc tcmalloc profiler)
2. 重新启动二进制
启动时需要配置的环境变量export LD_LIBRARY_PATH=/usr/local/lib64:$LD_LIBRARY_PATHexport TCMALLOC_SAMPLE_PARAMETER=524288export TCMALLOC_MAX_TOTAL_THREAD_CACHE_BYTES=134217728export MALLOC_CONF="prof:true"export PPROF_PATH="/bin/pprof"export HEAPCHECK=draconian./bin/${executebin} -m 0x7c00 -s 100 -r /var/tmp/server.sock >> log/control.log 2>&1 &
##3. 分析
方法一: 参考brpc里面的脚本:
perl ./rpc_data/profiling/pprof.pl --dot bin/test-server ./rpc_data/profiling/14deeb9b2dadc1b8739b869fd929781a/20211028.213732.heap 2>&1其中pprof.pl里的内容如下:./third-party/brpc/tools/pprof参考代码如下:./third-party/brpc/src/CMakeFiles/SOURCES_LIB.dir/brpc/builtin/pprof_perl.cpp.o./third-party/brpc/src/CMakeFiles/SOURCES_LIB.dir/brpc/builtin/pprof_service.cpp.o./third-party/brpc/src/brpc/builtin/pprof_perl.cpp./third-party/brpc/src/brpc/builtin/pprof_perl.h./third-party/brpc/src/brpc/builtin/pprof_service.h./third-party/brpc/src/brpc/builtin/pprof_service.cpp./third-party/brpc/tools/pprof./third-party/brpc/output/include/brpc/builtin/pprof_perl.h./third-party/brpc/output/include/brpc/builtin/pprof_service.h
方法二:直接使用brpc internal port 的内存页面进行剖析
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~