linux检测程序内存泄漏和内存错误

在linux的开发程序的时候,可以很方便的使用valgrind这个工具方便检测内存泄漏和内存错误。

安装很方便:

debian(如ubuntu)

sudo apt-get install valgrind

redhate系列(如centos)

sudo yum install valgrind

 

使用示例:

检测内存泄漏,

valgrind –leak-check=yes [application]

[application]就是你的程序名子。

输出结果如下:

==3122==
==3122== HEAP SUMMARY:
==3122==     in use at exit: 2,030 bytes in 6 blocks
==3122==   total heap usage: 25 allocs, 19 frees, 38,386,319 bytes allocated
==3122==
==3122== 583 bytes in 1 blocks are definitely lost in loss record 5 of 6
==3122==    at 0x4C2AC27: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3122==    by 0x40335C: Performance_AES_CBC(char const*, char const*, unsigned int) (in /home/liyi/Study/c_c++/ipptest/bin/main)
==3122==    by 0x403ED6: PerformanceTest() (in /home/liyi/Study/c_c++/ipptest/bin/main)
==3122==    by 0x401834: main (in /home/liyi/Study/c_c++/ipptest/bin/main)
==3122==
==3122== LEAK SUMMARY:
==3122==    definitely lost: 583 bytes in 1 blocks
==3122==    indirectly lost: 0 bytes in 0 blocks
==3122==      possibly lost: 0 bytes in 0 blocks
==3122==    still reachable: 1,447 bytes in 5 blocks
==3122==         suppressed: 0 bytes in 0 blocks
==3122== Reachable blocks (those to which a pointer was found) are not shown.
==3122== To see them, rerun with: –leak-check=full –show-reachable=yes
==3122==
==3122== For counts of detected and suppressed errors, rerun with: -v
==3122== Use –track-origins=yes to see where uninitialised values come from
==3122== ERROR SUMMARY: 7 errors from 7 contexts (suppressed: 2 from 2)

 

使用

–leak-check=full –show-reachable=yes

可以查看更加详细的内存信息。

 

更多的信息参看valgrind官方文档,

http://valgrind.org/docs/manual/quick-start.html#quick-start.intro

 

版权所有,禁止转载. 如需转载,请先征得博主的同意,并且表明文章出处,否则按侵权处理.

    分享到:

留言

你的邮箱是保密的 必填的信息用*表示