Ubuntu下安装Valgrind很简单,直接从ubuntu软件中心下载安装,并且还会提供一个简单的图形界面。。当然,你也可以在命令行中使用。
Valgrind实际上是一个工具集,包含多个调试工具,如常用的memcheck,当程序日渐庞大时,内存泄漏是个很头疼的问题,可以使用如下命令,来检测:
valgrind --tool=memcheck --leak-check=yes ./main
其中./main是需要检测的程序。当程序运行结束或中止时,会打印出内存信息。
这是本人运行程序过程中强制终止的信息:
=3516== HEAP SUMMARY:
==3516== in use at exit: 691,535 bytes in 912 blocks
==3516== total heap usage: 1,168 allocs, 256 frees, 2,524,201 bytes allocated
==3516==
==3516== 8 bytes in 1 blocks are definitely lost in loss record 180 of 587
==3516== at 0x4026864: malloc (vg_replace_malloc.c:236)
==3516== by 0x804D636: AnalyzeCBlock (AnalyzePage.c:1706)
==3516== by 0x804DA51: AnalyzePage (AnalyzePage.c:1825)
==3516== by 0x80645DC: GetTGPFromSoGouBySA (wrapper.c:1148)
==3516== by 0x8068BB6: ExeOneDS (wrapper.c:2847)
==3516== by 0x8068F98: ExecuteDataSource (wrapper.c:2972)
==3516== by 0x80541BC: SearchPlan (SearchPlan.c:263)
==3516== by 0x8054847: ThreadFunction_Regular (UserRegular.c:105)
==3516== by 0x8052C14: wrapper_fn (ThreadPool.c:100)
==3516== by 0x4063E98: start_thread (pthread_create.c:304)
==3516== by 0x41475FD: clone (clone.S:130)
==3516==
==3516== 8 bytes in 1 blocks are definitely lost in loss record 181 of 587
==3516== at 0x4026864: malloc (vg_replace_malloc.c:236)
==3516== by 0x804D636: AnalyzeCBlock (AnalyzePage.c:1706)
==3516== by 0x804DA51: AnalyzePage (AnalyzePage.c:1825)
==3516== by 0x8065A70: GetAlbumFromBaiduBySinger (wrapper.c:1602)
==3516== by 0x8068BB6: ExeOneDS (wrapper.c:2847)
==3516== by 0x8068D99: ExecuteDataSource (wrapper.c:2916)
==3516== by 0x80541BC: SearchPlan (SearchPlan.c:263)
==3516== by 0x8054847: ThreadFunction_Regular (UserRegular.c:105)
==3516== by 0x8052C14: wrapper_fn (ThreadPool.c:100)
==3516== by 0x4063E98: start_thread (pthread_create.c:304)
==3516== by 0x41475FD: clone (clone.S:130)
..........................
.......
==3516== LEAK SUMMARY:
==3516== definitely lost: 768 bytes in 13 blocks
==3516== indirectly lost: 3,728 bytes in 134 blocks
==3516== possibly lost: 544 bytes in 4 blocks
==3516== still reachable: 686,495 bytes in 761 blocks
==3516== suppressed: 0 bytes in 0 blocks
==3516== Reachable blocks (those to which a pointer was found) are not shown.
==3516== To see them, rerun with: --leak-check=full --show-reachable=yes
==3516==
==3516== For counts of detected and suppressed errors, rerun with: -v
==3516== Use --track-origins=yes to see where uninitialised values come from
==3516== ERROR SUMMARY: 32 errors from 24 contexts (suppressed: 33 from 8)
在最后会给出统计信息,32个内存错误,看样子,我有的忙了,不过幸好这个工具给出了具体出错的位置,不用花几天的功夫去检测!!!
以下是从网上摘录的
///////////////////////////////////////////////////////////////////////////////////////////
用法: valgrind [options] prog-and-args [options]: 常用选项,适用于所有Valgrind 工具
LOG信息输出
适用于Memcheck工具的相关选项: