linux下利用valgrind工具进行内存泄露检测和性能分析
http://blog.csdn.net/yanghao23/article/details/7514587
valgrind通常用來(lái)成分析程序性能及程序中的內(nèi)存泄露錯(cuò)誤
一?Valgrind工具集簡(jiǎn)紹
Valgrind包含下列工具:
????1、memcheck:檢查程序中的內(nèi)存問(wèn)題,如泄漏、越界、非法指針等。
????2、callgrind:檢測(cè)程序代碼的運(yùn)行時(shí)間和調(diào)用過(guò)程,以及分析程序性能。
????3、cachegrind:分析CPU的cache命中率、丟失率,用于進(jìn)行代碼優(yōu)化。
????4、helgrind:用于檢查多線程程序的競(jìng)態(tài)條件。
????5、massif:堆棧分析器,指示程序中使用了多少堆內(nèi)存等信息。
????6、lackey:
? ? 7、nulgrind:
這幾個(gè)工具的使用是通過(guò)命令:valgrand --tool=name 程序名來(lái)分別調(diào)用的,當(dāng)不指定tool參數(shù)時(shí)默認(rèn)是?--tool=memcheck
二?Valgrind工具詳解
1.Memcheck
????最常用的工具,用來(lái)檢測(cè)程序中出現(xiàn)的內(nèi)存問(wèn)題,所有對(duì)內(nèi)存的讀寫都會(huì)被檢測(cè)到,一切對(duì)malloc、free、new、delete的調(diào)用都會(huì)被捕獲。所以,它能檢測(cè)以下問(wèn)題:
???????1、對(duì)未初始化內(nèi)存的使用;
???????2、讀/寫釋放后的內(nèi)存塊;
???????3、讀/寫超出malloc分配的內(nèi)存塊;
???????4、讀/寫不適當(dāng)?shù)臈V袃?nèi)存塊;
???????5、內(nèi)存泄漏,指向一塊內(nèi)存的指針永遠(yuǎn)丟失;
???????6、不正確的malloc/free或new/delete匹配;
???????7、memcpy()相關(guān)函數(shù)中的dst和src指針重疊。
這些問(wèn)題往往是C/C++程序員最頭疼的問(wèn)題,Memcheck能在這里幫上大忙。
例如:
[plain]?view plaincopy
輸出結(jié)果如下:
==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????? // 內(nèi)存越界
==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) // 踩內(nèi)存
==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[] // 重復(fù)釋放
==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)?//由于非法指針賦值導(dǎo)致的程序崩潰
==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的檢測(cè)輸出結(jié)果看,這幾個(gè)錯(cuò)誤都找了出來(lái)。
?
2.Callgrind
????和gprof類似的分析工具,但它對(duì)程序的運(yùn)行觀察更是入微,能給我們提供更多的信息。和gprof不同,它不需要在編譯源代碼時(shí)附加特殊選項(xiàng),但加上調(diào)試選項(xiàng)是推薦的。Callgrind收集程序運(yùn)行時(shí)的一些數(shù)據(jù),建立函數(shù)調(diào)用關(guān)系圖,還可以有選擇地進(jìn)行cache模擬。在運(yùn)行結(jié)束時(shí),它會(huì)把分析數(shù)據(jù)寫入一個(gè)文件。callgrind_annotate可以把這個(gè)文件的內(nèi)容轉(zhuǎn)化成可讀的形式。
生成可視化的圖形需要下載gprof2dot:http://jrfonseca.googlecode.com/svn/trunk/gprof2dot/gprof2dot.py
這是個(gè)python腳本,把它下載之后修改其權(quán)限chmod +7?gprof2dot.py ,并把這個(gè)腳本添加到$PATH路徑中的任一文件夾下,我是將它放到了/usr/bin目錄下,這樣就可以直接在終端下執(zhí)行g(shù)prof2dot.py了。
? ?Callgrind可以生成程序性能分析的圖形,首先來(lái)說(shuō)說(shuō)程序性能分析的工具吧,通常可以使用gnu自帶的gprof,它的使用方法是:在編譯程序時(shí)添加-pg參數(shù),例如:
[plain]?view plaincopy
再執(zhí)行g(shù)prof ./tmp | gprof2dot.py |dot -Tpng -o report.png,打開 report.png結(jié)果:
顯示test被調(diào)用了5次,程序中耗時(shí)所占百分比最多的是test函數(shù)。
再來(lái)看?Callgrind的生成調(diào)用圖過(guò)程吧,執(zhí)行:valgrind --tool=callgrind ./tmp,執(zhí)行完成后在目錄下生成"callgrind.out.XXX"的文件這是分析文件,可以直接利用:callgrind_annotate callgrind.out.XXX 打印結(jié)果,也可以使用:gprof2dot.py -f callgrind callgrind.out.XXX?|dot -Tpng -o report.png 來(lái)生成圖形化結(jié)果:
它生成的結(jié)果非常詳細(xì),甚至連函數(shù)入口,及庫(kù)函數(shù)調(diào)用都標(biāo)識(shí)出來(lái)了。
3.Cachegrind
???????Cache分析器,它模擬CPU中的一級(jí)緩存I1,Dl和二級(jí)緩存,能夠精確地指出程序中cache的丟失和命中。如果需要,它還能夠?yàn)槲覀兲峁ヽache丟失次數(shù),內(nèi)存引用次數(shù),以及每行代碼,每個(gè)函數(shù),每個(gè)模塊,整個(gè)程序產(chǎn)生的指令數(shù)。這對(duì)優(yōu)化程序有很大的幫助。
????作一下廣告:valgrind自身利用該工具在過(guò)去幾個(gè)月內(nèi)使性能提高了25%-30%。據(jù)早先報(bào)道,kde的開發(fā)team也對(duì)valgrind在提高kde性能方面的幫助表示感謝。
它的使用方法也是:valgrind --tool=cachegrind 程序名,
4.Helgrind
????它主要用來(lái)檢查多線程程序中出現(xiàn)的競(jìng)爭(zhēng)問(wèn)題。Helgrind尋找內(nèi)存中被多個(gè)線程訪問(wèn),而又沒(méi)有一貫加鎖的區(qū)域,這些區(qū)域往往是線程之間失去同步的地方,而且會(huì)導(dǎo)致難以發(fā)掘的錯(cuò)誤。Helgrind實(shí)現(xiàn)了名為“Eraser”的競(jìng)爭(zhēng)檢測(cè)算法,并做了進(jìn)一步改進(jìn),減少了報(bào)告錯(cuò)誤的次數(shù)。不過(guò),Helgrind仍然處于實(shí)驗(yàn)階段。
首先舉一個(gè)競(jìng)態(tài)的例子吧:
[plain]?view plaincopy這段程序的 競(jìng)態(tài)在30~32行,我們想要的效果是3個(gè)線程分別對(duì)全局變量累加50次,最后全局變量的值為150,由于這里沒(méi)有加鎖,很明顯競(jìng)態(tài)使得程序不能達(dá)到我們的目標(biāo)。我們來(lái)看Helgrind是如何幫我們檢測(cè)到競(jìng)態(tài)的。 先編譯程序:gcc -o test thread.c -lpthread ,然后執(zhí)行:valgrind --tool=helgrind ./ test 輸出結(jié)果如下:
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
????堆棧分析器,它能測(cè)量程序在堆棧中使用了多少內(nèi)存,告訴我們堆塊,堆管理塊和棧的大小。Massif能幫助我們減少內(nèi)存的使用,在帶有虛擬內(nèi)存的現(xiàn)代系統(tǒng)中,它還能夠加速我們程序的運(yùn)行,減少程序停留在交換區(qū)中的幾率。
???????Massif對(duì)內(nèi)存的分配和釋放做profile。程序開發(fā)者通過(guò)它可以深入了解程序的內(nèi)存使用行為,從而對(duì)內(nèi)存使用進(jìn)行優(yōu)化。這個(gè)功能對(duì)C++尤其有用,因?yàn)?span face="Times New Roman">C++有很多隱藏的內(nèi)存分配和釋放。
此外,lackey和nulgrind也會(huì)提供。Lackey是小型工具,很少用到;Nulgrind只是為開發(fā)者展示如何創(chuàng)建一個(gè)工具。我們就不做介紹了。
三?使用Valgrind
???????Valgrind使用起來(lái)非常簡(jiǎn)單,你甚至不需要重新編譯你的程序就可以用它。當(dāng)然如果要達(dá)到最好的效果,獲得最準(zhǔn)確的信息,還是需要按要求重新編譯一下的。比如在使用memcheck的時(shí)候,最好關(guān)閉優(yōu)化選項(xiàng)。
???????valgrind命令的格式如下:
???????valgrind?[valgrind-options] your-prog [your-prog options]
valgrind --tool=massif --stacks=yes ./test
(這個(gè)工具有個(gè)bug, 只有程序中出現(xiàn)new或者malloc之類的堆操作,才會(huì)統(tǒng)計(jì)棧的使用,否則只統(tǒng)計(jì)堆的使用)
一些常用的選項(xiàng)如下:
| 選項(xiàng) | 作用 |
| -h --help | 顯示幫助信息。 |
| --version | 顯示valgrind內(nèi)核的版本,每個(gè)工具都有各自的版本。 |
| -q --quiet | 安靜地運(yùn)行,只打印錯(cuò)誤信息。 |
| -v --verbose | 打印更詳細(xì)的信息。 |
| --tool=<toolname> [default: memcheck] | 最常用的選項(xiàng)。運(yùn)行valgrind中名為toolname的工具。如果省略工具名,默認(rèn)運(yùn)行memcheck。 |
| --db-attach=<yes|no> [default: no] | 綁定到調(diào)試器上,便于調(diào)試錯(cuò)誤。 |
本文部分參考http://www.cnblogs.com/wangkangluo1/archive/2011/07/20/2111248.html
總結(jié)
以上是生活随笔為你收集整理的linux下利用valgrind工具进行内存泄露检测和性能分析的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 什么才是多线程安全的
- 下一篇: onTouchEvent