目錄html
便可即用linux
簡介程序員
安裝數組
使用緩存
檢測內存泄漏網絡
其餘內存問題多線程
其餘選項socket
一、沒有安裝的先在程序運行的主機上安裝,安裝教程見:1.4安裝。
二、使用:
命令:
valgrind --leak-check=full --log-file=leak.log /proc/path/proc_name
參數說明:--leak-check=full 信息顯示具體泄漏位置
--log-file=leak.log 將檢測信息輸入到日誌leak.log中
/proc/path/proc_name 須要檢測的程序
三、報錯信息和說明:
==29646== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 15 from 1)
==29646== malloc/free: in use at exit: 10 bytes in 1 blocks. //指示在程序退出時,還有多少內存沒有釋放。
==29646== malloc/free: 1 allocs, 0 frees, 10 bytes allocated. // 指示該執行過程malloc和free調用的次數。
==29646== For counts of detected errors, rerun with: -v // 提示若是要更詳細的信息,用-v選項。
==29646== searching for pointers to 1 not-freed blocks.
==29646== checked 56,164 bytes.
==29646==
==29646== LEAK SUMMARY:
==29646== definitely lost: 10 bytes in 1 blocks. //肯定的內存泄露(Definitely lost)
==29646== possibly lost: 0 bytes in 0 blocks. //可能的內存泄露(Possibly lost)
(發現了一個指向某塊內存中部的指針,而不是指向內存塊頭部。這種指針通常是原先指向內存塊頭部,後來移動到了內存塊的中部,還有可能該指針和該內存根本就沒有關係,檢測工具只是懷疑有內存泄漏)
==29646== still reachable: 0 bytes in 0 blocks.// still reachable: 表示泄漏的內存在程序運行完的時候,仍舊有指針指向它,於是,這種內存在程序運行結束以前能夠釋放。通常狀況下valgrind不會報這種泄漏,除非使用了參數 --show-reachable=yes。
==29646== suppressed: 0 bytes in 0 blocks.
==29646== Rerun with --leak-check=full to see details of leaked memory.
==29661== 10 bytes in 1 blocks are definitely lost in loss record 1 of 1
==29661== at 0x401A846: malloc (vg_replace_malloc.c:149)
==29661== by 0x804835D: main (memleak.c:6)
==29776== Invalid write of size 1 //堆內存越界被查出來(非法寫操做)
==29776== at 0x80483D2: main (invalidptr.c:7)
==29776== Address 0x4159034 is 2 bytes after a block of size 10 alloc'd
==29776== at 0x401A846: malloc (vg_replace_malloc.c:149)
==29776== by 0x80483C5: main (invalidptr.c:6)
==29776==
==29776== Source and destination overlap in memcpy(0x4159029, 0x4159028, 5) //踩內存
==29776== at 0x401C96D: memcpy (mc_replace_strmem.c:116)
==29776== by 0x80483E6: main (invalidptr.c:9)
==29776==
==29776== Invalid free() / delete / delete[] //重複釋放
==29776== at 0x401B3FB: free (vg_replace_malloc.c:233)
==29776== by 0x8048406: main (invalidptr.c:16)
==29776== Address 0x4159028 is 0 bytes inside a block of size 10 free'd
==29776== at 0x401B3FB: free (vg_replace_malloc.c:233)
==29776== by 0x80483F8: main (invalidptr.c:15)
==29776==
==29776== Use of uninitialised value of size 4 //非法指針(size 4 ,32bit設備),致使coredump
==29776== at 0x804840D: main (invalidptr.c:19)
==29776==
==29776== Process terminating with default action of signal 11 (SIGSEGV): dumping core
==29776== Bad permissions for mapped region at address 0x80482AD
==29776== at 0x804840D: main (invalidptr.c:19)
==29776==
https://www.ibm.com/developerworks/cn/linux/l-cn-valgrind/index.html
Valgrind是運行在Linux上一套基於仿真技術的程序調試和分析工具,是公認的最接近Purify的產品,它包含一個內核——一個軟件合成的CPU,和一系列的小工具,每一個工具均可以完成一項任務——調試,分析,或測試等。Valgrind能夠檢測內存泄漏和內存越界,還能夠分析cache的使用等,靈活輕巧而又強大。
一 Valgrind概觀
Valgrind的最新版是3.2.3,該版本包含下列工具:
一、memcheck:檢查程序中的內存問題,如泄漏、越界、非法指針等。
二、callgrind:檢測程序代碼覆蓋,以及分析程序性能。
三、cachegrind:分析CPU的cache命中率、丟失率,用於進行代碼優化。
四、helgrind:用於檢查多線程程序的競態條件。
五、massif:堆棧分析器,指示程序中使用了多少堆內存等信息。
六、lackey:
七、nulgrind:
1.Memcheck
最經常使用的工具,用來檢測程序中出現的內存問題,全部對內存的讀寫都會被檢測到,一切對malloc、free、new、delete的調用都會被捕獲。因此,它能檢測如下問題:
一、對未初始化內存的使用;
二、讀/寫釋放後的內存塊;
三、讀/寫超出malloc分配的內存塊;
四、讀/寫不適當的棧中內存塊;
五、內存泄漏,指向一塊內存的指針永遠丟失;
六、不正確的malloc/free或new/delete匹配;
七、memcpy()相關函數中的dst和src指針重疊。
這些問題每每是C/C++程序員最頭疼的問題,Memcheck能在這裏幫上大忙。
2.Callgrind
和gprof相似的分析工具,但它對程序的運行觀察更是入微,能給咱們提供更多的信息。和gprof不一樣,它不須要在編譯源代碼時附加特殊選項,但加上調試選項是推薦的。Callgrind收集程序運行時的一些數據,創建函數調用關係圖,還能夠有選擇地進行cache模擬。在運行結束時,它會把分析數據寫入一個文件。callgrind_annotate能夠把這個文件的內容轉化成可讀的形式。
說明:這個工具我也沒有用會,網上基本沒有找到有指導性的文檔,暫時留在後面慢慢研究吧。
3.Cachegrind
Cache分析器,它模擬CPU中的一級緩存I1,Dl和二級緩存,可以精確地指出程序中cache的丟失和命中。若是須要,它還可以爲咱們提供cache丟失次數,內存引用次數,以及每行代碼,每一個函數,每一個模塊,整個程序產生的指令數。這對優化程序有很大的幫助。
做一下廣告:valgrind自身利用該工具在過去幾個月內使性能提升了25%-30%。據早先報道,kde的開發team也對valgrind在提升kde性能方面的幫助表示感謝。
4.Helgrind
它主要用來檢查多線程程序中出現的競爭問題。Helgrind尋找內存中被多個線程訪問,而又沒有一向加鎖的區域,這些區域每每是線程之間失去同步的地方,並且會致使難以發掘的錯誤。Helgrind實現了名爲「Eraser」的競爭檢測算法,並作了進一步改進,減小了報告錯誤的次數。不過,Helgrind仍然處於實驗階段。
5. Massif
堆棧分析器,它能測量程序在堆棧中使用了多少內存,告訴咱們堆塊,堆管理塊和棧的大小。Massif能幫助咱們減小內存的使用,在帶有虛擬內存的現代系統中,它還可以加速咱們程序的運行,減小程序停留在交換區中的概率。
Massif對內存的分配和釋放作profile。程序開發者經過它能夠深刻了解程序的內存使用行爲,從而對內存使用進行優化。這個功能對C++尤爲有用,由於C++有不少隱藏的內存分配和釋放
此外,lackey和nulgrind也會提供。Lackey是小型工具,不多用到;Nulgrind只是爲開發者展現如何建立一個工具。咱們就不作介紹了。
valgrind下載:
http://valgrind.org/downloads/valgrind-3.12.0.tar.bz2
valgrind安裝:
1. tar -jxvf valgrind-3.12.0.tar.bz2
2. cd valgrind-3.12.0
3. ./configure
4. make
5. make install
輸入valgrind–h顯示valgrind的參數及提示,說明安裝成
Valgrind使用起來很是簡單,你甚至不須要從新編譯你的程序就能夠用它。固然若是要達到最好的效果,得到最準確的信息,仍是須要按要求從新編譯一下的。好比在使用memcheck的時候,最好關閉優化選項。
valgrind命令的格式以下:
valgrind [valgrind-options] your-prog [your-prog options]
一些經常使用的選項以下:
選項 |
做用
|
-h --help |
顯示幫助信息。
|
--version
|
顯示valgrind內核的版本,每一個工具都有各自的版本。
|
-q --quiet
|
安靜地運行,只打印錯誤信息。
|
-v --verbose
|
打印更詳細的信息。 |
--tool=<toolname> [default: memcheck]
|
最經常使用的選項。運行valgrind中名爲toolname的工具。若是省略工具名,默認運行memcheck。
|
--db-attach=<yes|no> [default: no] |
綁定到調試器上,便於調試錯誤。
|
示例代碼以下:
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
char *ptr;
ptr = (char *)malloc(10);
return 0;
}
保存爲memleak.c並編譯,而後用valgrind檢測。
$ gcc -o memleak memleak.c
(valgrind和purify最大的不一樣在於:valgrind只接管程序執行的過程,編譯時不須要valgrind干預,而purify會干預程序編譯過程)
$ valgrind --tool=memcheck ./memleak
咱們獲得以下錯誤信息:
[konten@tencent test_valgrind]$ valgrind ./memleak
==29646== Memcheck, a memory error detector.
==29646== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.
==29646== Using LibVEX rev 1732, a library for dynamic binary translation.
==29646== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
==29646== Using valgrind-3.2.3, a dynamic binary instrumentation framework.
==29646== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.
==29646== For more details, rerun with: -v
==29646==
==29646==
==29646== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 15 from 1)
==29646== malloc/free: in use at exit: 10 bytes in 1 blocks. //指示在程序退出時,還有多少內存沒有釋放。
==29646== malloc/free: 1 allocs, 0 frees, 10 bytes allocated. // 指示該執行過程malloc和free調用的次數。
==29646== For counts of detected errors, rerun with: -v // 提示若是要更詳細的信息,用-v選項。
==29646== searching for pointers to 1 not-freed blocks.
==29646== checked 56,164 bytes.
==29646==
==29646== LEAK SUMMARY:
==29646== definitely lost: 10 bytes in 1 blocks.
==29646== possibly lost: 0 bytes in 0 blocks.
==29646== still reachable: 0 bytes in 0 blocks.
==29646== suppressed: 0 bytes in 0 blocks.
==29646== Rerun with --leak-check=full to see details of leaked memory.
[konten@tencent test_valgrind]$
以上結果中,紅色的是手工添加的說明信息,其餘是valgrind的輸出。能夠看到,若是咱們僅僅用默認方式執行,valgrind只報告內存泄漏,但沒有顯示具體代碼中泄漏的地方。
所以咱們須要使用 「--leak-check=full」選項啓動 valgrind,咱們再執行一次:
[konten@tencent test_valgrind]$ valgrind --leak-check=full ./memleak
==29661== Memcheck, a memory error detector.
==29661== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.
==29661== Using LibVEX rev 1732, a library for dynamic binary translation.
==29661== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
==29661== Using valgrind-3.2.3, a dynamic binary instrumentation framework.
==29661== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.
==29661== For more details, rerun with: -v
==29661==
==29661==
==29661== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 15 from 1)
==29661== malloc/free: in use at exit: 10 bytes in 1 blocks.
==29661== malloc/free: 1 allocs, 0 frees, 10 bytes allocated.
==29661== For counts of detected errors, rerun with: -v
==29661== searching for pointers to 1 not-freed blocks.
==29661== checked 56,164 bytes.
==29661==
==29661== 10 bytes in 1 blocks are definitely lost in loss record 1 of 1
==29661== at 0x401A846: malloc (vg_replace_malloc.c:149)
==29661== by 0x804835D: main (memleak.c:6)
==29661==
==29661== LEAK SUMMARY:
==29661== definitely lost: 10 bytes in 1 blocks.
==29661== possibly lost: 0 bytes in 0 blocks.
==29661== still reachable: 0 bytes in 0 blocks.
==29661== suppressed: 0 bytes in 0 blocks.
[konten@tencent test_valgrind]$
和上次的執行結果基本相同,只是多了上面藍色的部分,指明瞭代碼中出現泄漏的具體位置。
以上就是用valgrind檢查內存泄漏的方法,用到的例子比較簡單,複雜的代碼最後結果也都同樣。
咱們下面的例子中包括常見的幾類內存問題:堆中的內存越界、踩內存、棧中的內存越界、非法指針使用、重複free。
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
char *ptr = malloc(10);
ptr[12] = 'a'; // 內存越界
memcpy(ptr +1, ptr, 5); // 踩內存
char a[10];
a[12] = 'i'; // 數組越界
free(ptr); // 重複釋放
free(ptr);
char *p1;
*p1 = '1'; // 非法指針
return 0;
}
編譯: gcc -o invalidptr invalidptr.c -g
執行:valgrind --leak-check=full ./invalidptr
結果以下:
[konten@tencent test_valgrind]$ valgrind --leak-check=full ./invalidptr
==29776== Memcheck, a memory error detector.
==29776== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.
==29776== Using LibVEX rev 1732, a library for dynamic binary translation.
==29776== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
==29776== Using valgrind-3.2.3, a dynamic binary instrumentation framework.
==29776== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.
==29776== For more details, rerun with: -v
==29776==
==29776== Invalid write of size 1 //堆內存越界被查出來
==29776== at 0x80483D2: main (invalidptr.c:7)
==29776== Address 0x4159034 is 2 bytes after a block of size 10 alloc'd
==29776== at 0x401A846: malloc (vg_replace_malloc.c:149)
==29776== by 0x80483C5: main (invalidptr.c:6)
==29776==
==29776== Source and destination overlap in memcpy(0x4159029, 0x4159028, 5) //踩內存
==29776== at 0x401C96D: memcpy (mc_replace_strmem.c:116)
==29776== by 0x80483E6: main (invalidptr.c:9)
==29776==
==29776== Invalid free() / delete / delete[] //重複釋放
==29776== at 0x401B3FB: free (vg_replace_malloc.c:233)
==29776== by 0x8048406: main (invalidptr.c:16)
==29776== Address 0x4159028 is 0 bytes inside a block of size 10 free'd
==29776== at 0x401B3FB: free (vg_replace_malloc.c:233)
==29776== by 0x80483F8: main (invalidptr.c:15)
==29776==
==29776== Use of uninitialised value of size 4
==29776== at 0x804840D: main (invalidptr.c:19)
==29776== //非法指針,致使coredump
==29776== Process terminating with default action of signal 11 (SIGSEGV): dumping core
==29776== Bad permissions for mapped region at address 0x80482AD
==29776== at 0x804840D: main (invalidptr.c:19)
==29776==
==29776== ERROR SUMMARY: 4 errors from 4 contexts (suppressed: 15 from 1)
==29776== malloc/free: in use at exit: 0 bytes in 0 blocks.
==29776== malloc/free: 1 allocs, 2 frees, 10 bytes allocated.
==29776== For counts of detected errors, rerun with: -v
==29776== All heap blocks were freed -- no leaks are possible.
Segmentation fault
[konten@tencent test_valgrind]$
從上面的結果看出,除了棧內存越界外,其餘常見的內存問題均可以用valgrind簡單的查出來。
三、顯示代碼覆蓋
用callgrind工具能方便的顯示程序執行的代碼覆蓋狀況。
看以下例子:
四、顯示線程競態條件 <該版本暫不支持>
用helgrind工具能夠在多線程代碼中找到可能產生競態條件的地方。
一、leak-check
--leak-check=<no|summary|yes|full> [default: summary]
用於控制內存泄漏檢測力度。
no,不檢測內存泄漏;
summary,僅報告總共泄漏的數量,不報告具體泄漏位置;
yes/full,報告泄漏總數、泄漏的具體位置。
二、show-reachable
--show-reachable=<yes|no> [default: no]
用於控制是否檢測控制範圍以外的泄漏,好比全局指針、static指針等。
#include <stdlib.h>
#include <stdio.h>
//char *gptr = NULL;
int main(void)
{
gptr = (char *)malloc(10);
return 0;
}
對應以上代碼,若--show-reachable爲no,則valgrind不報告內存泄漏,不然會報告。
三、undef-value-errors
--undef-value-errors=<yes|no> [default: yes]
用於控制是否檢測代碼中使用未初始化變量的狀況。
對應如下代碼:
int a;
printf("a = %d \n", a);
若 --undef-value-errors=no,則valgrind不報告錯誤,不然報告「Use of uninitialised value ...」的錯誤。
--log-file=filename 將結果輸出到文件。
--log-socket=192.168.0.1:12345 輸出到網絡。
--trace-children=<yes|no> [default: no]
--track-fds=<yes|no> [default: no]
--log-fd=<number> [default: 2, stderr]
--xml=<yes|no> [default: no]
--num-callers=<number> [default: 12]
--show-below-main=<yes|no> [default: no]
五 Valgrind的編譯安裝
一、下載源代碼,下載地址http://valgrind.org/downloads/current.html#current ,截止目前爲止,最新版本是3.2.3
二、編譯,在源代碼目錄下執行:
./configure --prefix=[你本身的安裝目錄]
make;make install
便好了。
三、配置缺省選項
valgrind提供3種方式用於設置缺省選項:
a、~/.valgrindrc文件;
b、環境變量$VALGRIND_OPTS;
c、當前目錄下的.valgrindrc文件;
優先順序爲 a、b、c
.valgrindrc的格式爲:
--ToolName:OptionName=OptionVal
如:
--memcheck:leak-check=yes
--memcheck:show-reachable=yes
purify
原文連接:https://blog.csdn.net/dengjin20104042056/article/details/103915097
程序中咱們定義了一個指針p,但並未給他分配空間,但咱們卻使用它了。
程序示例:
#include <stdio.h> #include <string.h> #include <stdlib.h> int main(void) { char ch; char *p; ch = *p; printf("ch = %c\n", ch); return 0; }
valgrind檢測出到咱們的程序使用了未初始化的變量。
p所指向的內存被釋放了,p變成了野指針,可是咱們卻繼續使用這片內存。
程序示例:
#include <stdio.h> #include <string.h> #include <stdlib.h> int main(void) { int *p = NULL; p = malloc(sizeof(int)); if (NULL == p) { printf("malloc failed...\n"); return 1; } memset(p, 0, sizeof(int)); *p = 88; printf("*p = %d\n", *p); //釋放內存 free(p); printf("*p = %d\n", *p); return 0; }
valgrind檢測到咱們使用了已經free的內存,並給出這片內存是哪裏分配和哪裏釋放的。
咱們動態地分配了一片連續的存儲空間,但咱們在訪問個數組時發生了越界訪問。
程序示例:
#include <stdio.h> #include <string.h> #include <stdlib.h> int main(void) { int i = 0; int *p = NULL; p = malloc(5 * sizeof(int)); if (NULL == p) { printf("malloc failed...\n"); return 1; } memset(p, 0, 10 * sizeof(int)); for (int i = 0; i <= 5; i++) { p[i] = i + 1; } for (int i = 0; i <= 5; i++) { printf("p[%d]: %d\n", i, p[i]); } return 0; }
valgrind檢測出越界信息以下。
注意:
valgrind不檢查非動態分配數組的使用狀況。
內存泄漏的緣由在於咱們使用free或者new分配空間以後,沒有使用free或者delete釋放內存。
程序示例:
#include <stdio.h> #include <string.h> #include <stdlib.h> int main(void) { int *p = NULL; p = malloc(sizeof(int)); *p = 88; printf("*p = %d\n", *p); return 0; }
valgrind的記錄顯示上面的程序用了1次malloc,卻調用了0次free。
可使用–leak-check=full進一步獲取內存泄漏的信息,好比malloc具體行號。
通常咱們使用malloc分配的空間,必須使用free釋放內存。使用new分配的空間,使用delete釋放內存。
程序示例:
#include <stdio.h> #include <string.h> #include <stdlib.h> int main(void) { int *p = NULL; p = (int *)malloc(sizeof(int)); *p = 88; printf("*p = %d\n", *p); delete p; return 0; }
不匹配地使用malloc/new/new[] 和 free/delete/delete[]則會被提示mismacth
通常狀況下,內存分配一次,只釋放一次。若是屢次釋放,可能會出現double free。
程序示例:
#include <stdio.h> #include <string.h> #include <stdlib.h> int main(void) { int *p = NULL; p = (int *)malloc(sizeof(int)); *p = 88; printf("*p = %d\n", *p); free p; free p; return 0; }
屢次釋放同一內存,出現非法釋放內存。
09. 總結
內存泄露問題很是難定位,對於小工程項目來講,簡單去檢查代碼中new和delete的匹配對數就基本能定位到問題,可是一旦代碼量上升到以萬單位時,僅靠肉眼檢查來定位問題那就很是困難了,因此咱們須要利用工具幫助咱們找出問題所在。在Linux系統下內存檢測工具首推Valgrind,一款很是好用的開源內存管理工具。Valgrind實際上是一個工具集,內存錯誤檢測只是它衆多功能的一個,但咱們用得最多的功能正是它——memcheck。
總之,valgrind工具能夠檢測下列與內存相關的問題 :
未釋放內存的使用 對釋放後內存的讀/寫 對已分配內存塊尾部的讀/寫 內存泄露 不匹配的使用malloc/new/new[] 和 free/delete/delete[] 重複釋放內存 10. 附錄