Linux C編程內存泄漏工具:memwatch的使用

1.首先你須要下載它,在http://www.linkdata.se/sourcecode/memwatch/ide

2.該內存工具是開源的(我也很喜歡開源^_^...),無需安裝,只要在你的代碼中包含它的頭文件.像這樣 #include "memwatch"工具

3.目前用到的最新版本是memwatch-2.71.tar.gz測試

4.解壓 tar -xvf memwatch-2.71.tar.gzspa

5.在解壓文件中將memwatch.c和memwatch.h拷貝到你的程序目錄中3d

6.測試文件中包含testmem.c , memwatch.c, memwatch.hcode

7.測試代碼:blog

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <errno.h>
 4 #include "memwatch.h"
 5 int main()
 6 {
 7         char *hello;
 8         setenv("MALLOC_TRACE","output",1);
 9         mtrace();
10         if((hello = (char *)malloc(sizeof(char))) == NULL)
11         {
12                 perror("Cannot allocate memory");
13                 return -1;
14         }
15         return 0;
16 }
View Code

8.測試命令:內存

gcc -DMEMWATCH -DMW_STDIO testmem.c memwatch.c -o testmemget

9.運行 ./testmemio

10.生成memwatch.log,並查看cat memwatch.log

至於什麼樣的原理,你們能夠讀讀裏面的源代碼和README文件

相關文章
相關標籤/搜索