linux下利用valgrind工具进行内存泄露检测和性能分析
http://blog.csdn.net/yanghao23/article/details/7514587
valgrind通常用來成分析程序性能及程序中的內存泄露錯誤
一?Valgrind工具集簡紹
Valgrind包含下列工具:
????1、memcheck:檢查程序中的內存問題,如泄漏、越界、非法指針等。
????2、callgrind:檢測程序代碼的運行時間和調用過程,以及分析程序性能。
????3、cachegrind:分析CPU的cache命中率、丟失率,用于進行代碼優化。
????4、helgrind:用于檢查多線程程序的競態條件。
????5、massif:堆棧分析器,指示程序中使用了多少堆內存等信息。
????6、lackey:
? ? 7、nulgrind:
這幾個工具的使用是通過命令:valgrand --tool=name 程序名來分別調用的,當不指定tool參數時默認是?--tool=memcheck
二?Valgrind工具詳解
1.Memcheck
????最常用的工具,用來檢測程序中出現的內存問題,所有對內存的讀寫都會被檢測到,一切對malloc、free、new、delete的調用都會被捕獲。所以,它能檢測以下問題:
???????1、對未初始化內存的使用;
???????2、讀/寫釋放后的內存塊;
???????3、讀/寫超出malloc分配的內存塊;
???????4、讀/寫不適當的棧中內存塊;
???????5、內存泄漏,指向一塊內存的指針永遠丟失;
???????6、不正確的malloc/free或new/delete匹配;
???????7、memcpy()相關函數中的dst和src指針重疊。
這些問題往往是C/C++程序員最頭疼的問題,Memcheck能在這里幫上大忙。
例如:
[plain]?view plaincopy
輸出結果如下:
==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的檢測輸出結果看,這幾個錯誤都找了出來。
?
2.Callgrind
????和gprof類似的分析工具,但它對程序的運行觀察更是入微,能給我們提供更多的信息。和gprof不同,它不需要在編譯源代碼時附加特殊選項,但加上調試選項是推薦的。Callgrind收集程序運行時的一些數據,建立函數調用關系圖,還可以有選擇地進行cache模擬。在運行結束時,它會把分析數據寫入一個文件。callgrind_annotate可以把這個文件的內容轉化成可讀的形式。
生成可視化的圖形需要下載gprof2dot:http://jrfonseca.googlecode.com/svn/trunk/gprof2dot/gprof2dot.py
這是個python腳本,把它下載之后修改其權限chmod +7?gprof2dot.py ,并把這個腳本添加到$PATH路徑中的任一文件夾下,我是將它放到了/usr/bin目錄下,這樣就可以直接在終端下執行gprof2dot.py了。
? ?Callgrind可以生成程序性能分析的圖形,首先來說說程序性能分析的工具吧,通常可以使用gnu自帶的gprof,它的使用方法是:在編譯程序時添加-pg參數,例如:
[plain]?view plaincopy
再執行gprof ./tmp | gprof2dot.py |dot -Tpng -o report.png,打開 report.png結果:
顯示test被調用了5次,程序中耗時所占百分比最多的是test函數。
再來看?Callgrind的生成調用圖過程吧,執行:valgrind --tool=callgrind ./tmp,執行完成后在目錄下生成"callgrind.out.XXX"的文件這是分析文件,可以直接利用:callgrind_annotate callgrind.out.XXX 打印結果,也可以使用:gprof2dot.py -f callgrind callgrind.out.XXX?|dot -Tpng -o report.png 來生成圖形化結果:
它生成的結果非常詳細,甚至連函數入口,及庫函數調用都標識出來了。
3.Cachegrind
???????Cache分析器,它模擬CPU中的一級緩存I1,Dl和二級緩存,能夠精確地指出程序中cache的丟失和命中。如果需要,它還能夠為我們提供cache丟失次數,內存引用次數,以及每行代碼,每個函數,每個模塊,整個程序產生的指令數。這對優化程序有很大的幫助。
????作一下廣告:valgrind自身利用該工具在過去幾個月內使性能提高了25%-30%。據早先報道,kde的開發team也對valgrind在提高kde性能方面的幫助表示感謝。
它的使用方法也是:valgrind --tool=cachegrind 程序名,
4.Helgrind
????它主要用來檢查多線程程序中出現的競爭問題。Helgrind尋找內存中被多個線程訪問,而又沒有一貫加鎖的區域,這些區域往往是線程之間失去同步的地方,而且會導致難以發掘的錯誤。Helgrind實現了名為“Eraser”的競爭檢測算法,并做了進一步改進,減少了報告錯誤的次數。不過,Helgrind仍然處于實驗階段。
首先舉一個競態的例子吧:
[plain]?view plaincopy這段程序的 競態在30~32行,我們想要的效果是3個線程分別對全局變量累加50次,最后全局變量的值為150,由于這里沒有加鎖,很明顯競態使得程序不能達到我們的目標。我們來看Helgrind是如何幫我們檢測到競態的。 先編譯程序:gcc -o test thread.c -lpthread ,然后執行:valgrind --tool=helgrind ./ test 輸出結果如下:
49c0b70: 1?
49c0b70: 2?
==4666== Thread #3 was created
==4666==??? at 0x412E9D8: clone (clone.S:111)
==4666==??? by 0x40494B5: pthread_create@@GLIBC_2.1 (createthread.c:256)
==4666==??? by 0x4026E2D: pthread_create_WRK (hg_intercepts.c:257)
==4666==??? by 0x4026F8B: pthread_create@* (hg_intercepts.c:288)
==4666==??? by 0x8048524: main (in /home/yanghao/Desktop/testC/testmem/a.out)
==4666==?
==4666== Thread #2 was created
==4666==??? at 0x412E9D8: clone (clone.S:111)
==4666==??? by 0x40494B5: pthread_create@@GLIBC_2.1 (createthread.c:256)
==4666==??? by 0x4026E2D: pthread_create_WRK (hg_intercepts.c:257)
==4666==??? by 0x4026F8B: pthread_create@* (hg_intercepts.c:288)
==4666==??? by 0x8048500: main (in /home/yanghao/Desktop/testC/testmem/a.out)
==4666==?
==4666== Possible data race during read of size 4 at 0x804a028 by thread #3
==4666==??? at 0x804859C:?threadfn?(in /home/yanghao/Desktop/testC/testmem/a.out)
==4666==??? by 0x4026F60: mythread_wrapper (hg_intercepts.c:221)
==4666==??? by 0x4048E98: start_thread (pthread_create.c:304)
==4666==??? by 0x412E9ED: clone (clone.S:130)
==4666==? This conflicts with a previous write of size 4 by thread #2
==4666==??? at 0x80485CA: threadfn (in /home/yanghao/Desktop/testC/testmem/a.out)
==4666==??? by 0x4026F60: mythread_wrapper (hg_intercepts.c:221)
==4666==??? by 0x4048E98: start_thread (pthread_create.c:304)
==4666==??? by 0x412E9ED: clone (clone.S:130)
==4666==?
==4666== Possible data race during write of size 4 at 0x804a028 by thread #2
==4666==??? at 0x80485CA: threadfn (in /home/yanghao/Desktop/testC/testmem/a.out)
==4666==??? by 0x4026F60: mythread_wrapper (hg_intercepts.c:221)
==4666==??? by 0x4048E98: start_thread (pthread_create.c:304)
==4666==??? by 0x412E9ED: clone (clone.S:130)
==4666==? This conflicts with a previous read of size 4 by thread #3
==4666==??? at 0x804859C: threadfn (in /home/yanghao/Desktop/testC/testmem/a.out)
==4666==??? by 0x4026F60: mythread_wrapper (hg_intercepts.c:221)
==4666==??? by 0x4048E98: start_thread (pthread_create.c:304)
==4666==??? by 0x412E9ED: clone (clone.S:130)
==4666==?
49c0b70: 3?
......
55c1b70: 51?
==4666==?
==4666== For counts of detected and suppressed errors, rerun with: -v
==4666== Use --history-level=approx or =none to gain increased speed, at
==4666== the cost of reduced accuracy of conflicting-access information
==4666== ERROR SUMMARY: 8 errors from 2 contexts (suppressed: 99 from 31)
5. Massif
????堆棧分析器,它能測量程序在堆棧中使用了多少內存,告訴我們堆塊,堆管理塊和棧的大小。Massif能幫助我們減少內存的使用,在帶有虛擬內存的現代系統中,它還能夠加速我們程序的運行,減少程序停留在交換區中的幾率。
???????Massif對內存的分配和釋放做profile。程序開發者通過它可以深入了解程序的內存使用行為,從而對內存使用進行優化。這個功能對C++尤其有用,因為C++有很多隱藏的內存分配和釋放。
此外,lackey和nulgrind也會提供。Lackey是小型工具,很少用到;Nulgrind只是為開發者展示如何創建一個工具。我們就不做介紹了。
三?使用Valgrind
???????Valgrind使用起來非常簡單,你甚至不需要重新編譯你的程序就可以用它。當然如果要達到最好的效果,獲得最準確的信息,還是需要按要求重新編譯一下的。比如在使用memcheck的時候,最好關閉優化選項。
???????valgrind命令的格式如下:
???????valgrind?[valgrind-options] your-prog [your-prog options]
valgrind --tool=massif --stacks=yes ./test
(這個工具有個bug, 只有程序中出現new或者malloc之類的堆操作,才會統計棧的使用,否則只統計堆的使用)
一些常用的選項如下:
| 選項 | 作用 |
| -h --help | 顯示幫助信息。 |
| --version | 顯示valgrind內核的版本,每個工具都有各自的版本。 |
| -q --quiet | 安靜地運行,只打印錯誤信息。 |
| -v --verbose | 打印更詳細的信息。 |
| --tool=<toolname> [default: memcheck] | 最常用的選項。運行valgrind中名為toolname的工具。如果省略工具名,默認運行memcheck。 |
| --db-attach=<yes|no> [default: no] | 綁定到調試器上,便于調試錯誤。 |
本文部分參考http://www.cnblogs.com/wangkangluo1/archive/2011/07/20/2111248.html
總結
以上是生活随笔為你收集整理的linux下利用valgrind工具进行内存泄露检测和性能分析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 什么才是多线程安全的
- 下一篇: linux 其他常用命令