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

  • 相关阅读:
    Mysql数据库快速备份还原-mysqldump
    写给年轻人的交友和人脉建议
    令人担忧的趋势:科技崇拜与人文失落
    高情商的特征
    高情商与朋友圈
    数据库临时表空间设置
    oracle 临时表空间的增删改查
    语言表达能力写作能力决定一个人的发展和未来
    一个人如何从平庸到优秀,再到卓越?
    06.堆排序
  • 原文地址:https://www.cnblogs.com/jinyz/p/3068553.html
Copyright © 2020-2023  润新知