歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Ubuntu下安裝內存檢測工具Valgrind

Ubuntu下安裝內存檢測工具Valgrind

日期:2017/2/28 16:03:02   编辑:Linux教程

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 工具

  1. -tool=<name> 最常用的選項。運行 valgrind 中名為toolname的工具。默認memcheck。
  2. h –help 顯示幫助信息。
  3. -version 顯示valgrind 內核的版本,每個工具都有各自的版本。
  4. q –quiet 安靜地運行,只打印錯誤信息。
  5. v –verbose 更詳細的信息, 增加錯誤數統計。
  6. -trace-children=no|yes 跟蹤子線程? [no]
  7. -track-fds=no|yes 跟蹤打開的文件描述?[no]
  8. -time-stamp=no|yes 增加時間戳到LOG信息? [no]
  9. -log-fd=<number> 輸出LOG到描述符文件 [2=stderr]
  10. -log-file=<file> 將輸出的信息寫入到filename.PID的文件裡,PID是運行程序的進行ID
  11. -log-file-exactly=<file> 輸出LOG信息到 file
  12. -log-file-qualifier=<VAR> 取得環境變量的值來做為輸出信息的文件名。 [none]
  13. -log-socket=ipaddr:port 輸出LOG到socket ,ipaddr:port

LOG信息輸出

  1. -xml=yes 將信息以xml格式輸出,只有memcheck可用
  2. -num-callers=<number> show <number> callers in stack traces [12]
  3. -error-limit=no|yes 如果太多錯誤,則停止顯示新錯誤? [yes]
  4. -error-exitcode=<number> 如果發現錯誤則返回錯誤代碼 [0=disable]
  5. -db-attach=no|yes 當出現錯誤,valgrind 會自動啟動調試器gdb。[no]
  6. -db-command=<command> 啟動調試器的命令行選項[gdb -nw %f %p]

適用於Memcheck工具的相關選項:

  1. -leak-check=no|summary|full 要求對leak給出詳細信息? [summary]
  2. -leak-resolution=low|med|high how much bt merging in leak check [low]
  3. -show-reachable=no|yes show reachable blocks in leak check? [no]
Copyright © Linux教程網 All Rights Reserved