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 }
8.测试命令:内存
gcc -DMEMWATCH -DMW_STDIO testmem.c memwatch.c -o testmemget
9.运行 ./testmemio
10.生成memwatch.log,并查看cat memwatch.log
至于什么样的原理,你们能够读读里面的源代码和README文件