valgrind是一款用於記憶體除錯、記憶體洩漏檢測以及效能分析的軟體開發工具。valgrind這個名字取自北歐神話中英靈殿的入口。
valgrind的最初作者是julian seward,他於2023年由於在開發valgrind上的工作獲得了第二屆google-o'reilly開源**獎。
valgrind遵守gnu通用公共許可證條款,是一款自由軟體。
到3.6.1版本為止,valgrind支援-linuxand -darwin (mac os x).
[root@localhost~]#tar–jxvf valgrind-3.2.3.tar.bz2
[root@localhost ~]# cd valgrind-3.3.1
[root@localhost valgrind-3.3.1]# ./configure && make && makeinstall
valgrind [options] prog-and-args [options]:常用選項,適用於所有valgrind工具
-tool=最常用的選項。執行 valgrind中名為toolname的工具。預設memcheck。
h –help 顯示幫助資訊。
-version 顯示valgrind核心的版本,每個工具都有各自的版本。
q –quiet 安靜地執行,只列印錯誤資訊。
v –verbose 更詳細的資訊, 增加錯誤數統計。
-trace-children=no|yes 跟蹤子執行緒? [no]
-track-fds=no|yes 跟蹤開啟的檔案描述?[no]
-time-stamp=no|yes 增加時間戳到log資訊? [no]
-log-fd=輸出log到描述符檔案 [2=stderr]
-log-file=將輸出的資訊寫入到filename.pid的檔案裡,pid是執行程式的進行id
-log-file-exactly=輸出log資訊到 file
-log-file-qualifier=取得環境變數的值來做為輸出資訊的檔名。 [none]
-log-socket=ipaddr:port 輸出log到socket ,ipaddr:port
log資訊輸出
-xml=yes 將資訊以xml格式輸出,只有memcheck可用
-num-callers=showcallers in stack traces [12]
-error-limit=no|yes 如果太多錯誤,則停止顯示新錯誤? [yes]
-error-exitcode=如果發現錯誤則返回錯誤** [0=disable]
-db-attach=no|yes 當出現錯誤,valgrind會自動啟動偵錯程式gdb。[no]
-db-command=啟動偵錯程式的命令列選項[gdb -nw %f %p]
-leak-check=no|summary|full 要求對leak給出詳細資訊? [summary]
-leak-resolution=low|med|high how much btmerging in leak check [low]
-show-reachable=no|yes show reachableblocks in leak check? [no]
使用未初始化的記憶體 (useof uninitialised memory)
使用已經釋放了的記憶體(reading/writing memory after it has been free』d)
使用超過 malloc分配的記憶體空間(reading/writing off the endof malloc』d blocks)
申請的空間是否有釋放(memory leaks – wherepointers to malloc』d blocks are lost
forever)
malloc/free/new/delete申請和釋放記憶體的匹配(mismatched use ofmalloc/new/new vs
free/delete/delete )
#include #include int main()
kk,*p;
kk.a = 10;
printf("kk.a : %d \n",kk.a);
p = (struct mm *)malloc(sizeof(struct mm));
p->a = 9;
printf("p->a : %d \n",p->a);
return 0;
}
操作如下:
[root@localhost ~]# vim hello.c
[root@localhost ~]# gcc -g -o hello hello.c
[root@localhost ~]# ./hello
結果如下:
然後用valgrind命令:
[root@localhost ~]# valgrind --tool=memcheck--leak-check=yes --show-reachable=yes ./hello
可以看出上面提示「malloc/free:1 allocs, 0 frees, 8 bytes allocated.」,「definitely
lost: 8 bytes in 1 blocks.」。即丟失了8個位元組。
使用valgrind工具檢測記憶體洩漏
因為公司伺服器上面本身就安裝有valgrind,所以我是直接拿過來使用的,並沒有自己安裝過,需要安裝的朋友自己google安裝吧。可以使用 which valgrind 命令來看自己電腦上是否已經安裝上valgrind。useful link 3.1 valgrind 使用方法,參考這裡 基本的命令...
Valgrind 記憶體檢測工具
valgrind是乙個gpl的軟體,用於linux for x86,amd64 and ppc32 程式的記憶體除錯和 剖析。你可以在它的環境中執行你的程式來監視記憶體的使用情況,比如c 語言中的malloc和free或者 c 中的new和 delete。使用valgrind的工具包,你可以自動的檢...
記憶體檢測工具Valgrind
valgrind是一套linux下,開放源 gpl v2 的 除錯工具的集合。valgrind由核心 core 以及基於核心的其他除錯工具組成。核心類似於乙個框架 framework 它模擬了乙個cpu環境,並提供服務給其他工具 而其他工具則類似於外掛程式 plug in 利用核心提供的服務完成各種...