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個內存錯誤,看樣子,www.linuxidc.com我有的忙了,不過幸好這個工具給出了具體出錯的位置,不用花幾天的功夫去檢測!!!
以下是從網上摘錄的
///////////////////////////////////////////////////////////////////////////////////////////
用法: valgrind [options] prog-and-args [options]: 常用選項,適用於所有Valgrind 工具
LOG信息輸出
適用於Memcheck工具的相關選項: