内存泄露bug指南(二)

[root@s144 src]# env HEAPCHECK=normal /root/src/test_memleak
WARNING: Perftools heap leak checker is active -- Performance may suffer
An error occurred.Skipping final stage
Leak check _main_ detected leaks of 404 bytes in 1 objects
The 1 largest leaks:
Using local file /root/src/test_memleak.
Leak of 404 bytes in 1 objects allocated from:
        @ 4007ff main
        @ 3d8dc1d994 __libc_start_main

If the preceding stack traces are not enough to find the leaks, try running THIS shell command:

pprof /root/src/test_memleak "/tmp/test_memleak.25383._main_-end.heap" --inuse_objects --lines --heapcheck  --edgefraction=1e-10 --nodefraction=1e-10 --gv

If you are still puzzled about why the leaks are there, try rerunning this program with HEAP_CHECK_TEST_POINTER_ALIGNMENT=1 and/or with HEAP_CHECK_MAX_POINTER_OFFSET=-1
If the leak report occurs in a small fraction of runs, try running with TCMALLOC_MAX_FREE_QUEUE_SIZE of few hundred MB or with TCMALLOC_RECLAIM_MEMORY=false, it might help fi
Exiting with error code (instead of crashing) because of whole-program memory leaks

 上面的报告显示有404个字节的内存泄漏,并提示使用pprof进一步跟踪泄漏来源的方法。

[root@s144 src]# pprof /root/src/test_memleak "/tmp/test_memleak.25383._main_-end.heap" --inuse_objects --lines --heapcheck  --edgefraction=1e-10 --nodefraction=1e-10 --text
Using local file /root/src/test_memleak.
Using local file /tmp/test_memleak.25383._main_-end.heap.
Total: 1 objects
       1 100.0% 100.0%        1 100.0% main /root/src/test_memleak.cpp:14
       0   0.0% 100.0%        1 100.0% __libc_start_main ??:0

根据上面提示,锁定具体的代码文件及代码行,协助开发快速定位到内存泄露问题bug。当然这是很简单的例子,还得经得起实际应用程序的考验,帮助开发成长避免重复错误,拿饮食行业比喻,想成为美食评估师,而不是清洁服务员。

最后,补充一点,除了前面使用env命令行的全局内存泄漏检查方式外,还可以作对代码段的更加细粒度的泄漏检查。这里考虑不修改源代码的前提下检查内存泄露,所以暂时不考虑。好的 C/C++程序员,应该做到自己的程序都能优雅退出,这是最基本的要求。这里所谓的优雅退出,指的是退出的时候把该释放的资源释放掉,且保证程序退出时不挂。

 

原文链接: https://www.cnblogs.com/jinyz/archive/2013/05/09/3068553.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    内存泄露bug指南(二)

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/87829

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月9日 下午11:17
下一篇 2023年2月9日 下午11:17

相关推荐