• 内存泄露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++程序员,应该做到自己的程序都能优雅退出,这是最基本的要求。这里所谓的优雅退出,指的是退出的时候把该释放的资源释放掉,且保证程序退出时不挂。

  • 相关阅读:
    Java基础教程(15)--枚举类型
    Java基础教程(14)--嵌套类
    Java基础教程(13)--包
    Java基础教程(12)--深入理解类
    Java基础教程(11)--对象
    Java基础教程(10)--类
    Java基础教程(9)--流程控制
    Java基础教程(8)--表达式、语句和块
    Java基础教程(7)--运算符
    Java基础教程(6)--数组
  • 原文地址:https://www.cnblogs.com/jinyz/p/3068553.html
Copyright © 2020-2023  润新知