第一步:程序員
獲取Valgrind 包 能夠先使用瀏覽器訪問http://valgrind.org/downloads/查看當前版本瀏覽器
wget http://valgrind.org/downloads/valgrind-3.10.1.tar.bz2多線程
第二步:app
將下載的文件解壓ide
tar -jxvf valgrind-3.10.1.tar.bz2函數
第三步:工具
安裝和配置
./autogen.sh
./configure性能
make; make install優化
Linux下利用Valgrind工具進行內存泄露檢測和性能分析
Valgrind一般用來成分析程序性能及程序中的內存泄露錯誤spa
一 Valgrind工具集簡紹
Valgrind包含下列工具:
一、memcheck:檢查程序中的內存問題,如泄漏、越界、非法指針等。
二、callgrind:檢測程序代碼的運行時間和調用過程,以及分析程序性能。
三、cachegrind:分析CPU的cache命中率、丟失率,用於進行代碼優化。
四、helgrind:用於檢查多線程程序的競態條件。
五、massif:堆棧分析器,指示程序中使用了多少堆內存等信息。
六、lackey:
七、nulgrind:
這幾個工具的使用是經過命令:valgrand --tool=name 程序名來分別調用的,當不指定tool參數時默認是 --tool=memcheck
二 Valgrind工具詳解
1.Memcheck
最經常使用的工具,用來檢測程序中出現的內存問題,全部對內存的讀寫都會被檢測到,一切對malloc、free、new、delete的調用都會被捕獲。因此,它能檢測如下問題:
一、對未初始化內存的使用;
二、讀/寫釋放後的內存塊;
三、讀/寫超出malloc分配的內存塊;
四、讀/寫不適當的棧中內存塊;
五、內存泄漏,指向一塊內存的指針永遠丟失;
六、不正確的malloc/free或new/delete匹配;
七、memcpy()相關函數中的dst和src指針重疊。
這些問題每每是C/C++程序員最頭疼的問題,Memcheck能在這裏幫上大忙。
例如:
- #include <stdlib.h>
- #include <malloc.h>
- #include <string.h>
-
- void test()
- {
- int *ptr = malloc(sizeof(int)*10);
-
- ptr[10] = 7; // 內存越界
-
- memcpy(ptr +1, ptr, 5); // 踩內存
-
-
- free(ptr);
- free(ptr);// 重複釋放
-
- int *p1;
- *p1 = 1; // 非法指針
- }
-
- int main(void)
- {
- test();
- return 0;
- }
將程序編譯生成可執行文件後執行:valgrind --leak-check=full ./程序名
輸出結果以下:
- ==4832== Memcheck, a memory error detector
- ==4832== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
- ==4832== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info
- ==4832== Command: ./tmp
- ==4832==
- ==4832== Invalid write of size 4 // 內存越界
- ==4832== at 0x804843F: test (in /home/yanghao/Desktop/testC/testmem/tmp)
- ==4832== by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)
- ==4832== Address 0x41a6050 is 0 bytes after a block of size 40 alloc'd
- ==4832== at 0x4026864: malloc (vg_replace_malloc.c:236)
- ==4832== by 0x8048435: test (in /home/yanghao/Desktop/testC/testmem/tmp)
- ==4832== by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)
- ==4832==
- ==4832== Source and destination overlap in memcpy(0x41a602c, 0x41a6028, 5) // 踩內存
- ==4832== at 0x4027BD6: memcpy (mc_replace_strmem.c:635)
- ==4832== by 0x8048461: test (in /home/yanghao/Desktop/testC/testmem/tmp)
- ==4832== by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)
- ==4832==
- ==4832== Invalid free() / delete / delete[] // 重複釋放
- ==4832== at 0x4025BF0: free (vg_replace_malloc.c:366)
- ==4832== by 0x8048477: test (in /home/yanghao/Desktop/testC/testmem/tmp)
- ==4832== by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)
- ==4832== Address 0x41a6028 is 0 bytes inside a block of size 40 free'd
- ==4832== at 0x4025BF0: free (vg_replace_malloc.c:366)
- ==4832== by 0x804846C: test (in /home/yanghao/Desktop/testC/testmem/tmp)
- ==4832== by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)
- ==4832==
- ==4832== Use of uninitialised value of size 4 // 非法指針
- ==4832== at 0x804847B: test (in /home/yanghao/Desktop/testC/testmem/tmp)
- ==4832== by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)
- ==4832==
- ==4832==
- ==4832== Process terminating with default action of signal 11 (SIGSEGV) //因爲非法指針賦值致使的程序崩潰
- ==4832== Bad permissions for mapped region at address 0x419FFF4
- ==4832== at 0x804847B: test (in /home/yanghao/Desktop/testC/testmem/tmp)
- ==4832== by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)
- ==4832==
- ==4832== HEAP SUMMARY:
- ==4832== in use at exit: 0 bytes in 0 blocks
- ==4832== total heap usage: 1 allocs, 2 frees, 40 bytes allocated
- ==4832==
- ==4832== All heap blocks were freed -- no leaks are possible
- ==4832==
- ==4832== For counts of detected and suppressed errors, rerun with: -v
- ==4832== Use --track-origins=yes to see where uninitialised values come from
- ==4832== ERROR SUMMARY: 4 errors from 4 contexts (suppressed: 11 from 6)
- Segmentation fault
從valgrind的檢測輸出結果看,這幾個錯誤都找了出來。