Linux 内存检测工具 memwatch的使用

1. 官网下载memwatch的源码

    推荐使用地址http://www.linkdata.se/sourcecode/memwatch/

    


2. linux环境下压缩包解压

      使用命令:tar -xzvf memwatch-2.71.tar.gz

       查看memwatch目录结构:

       

       结构说明:

       makefile 编译使用不再多说

       USING 说明memwatch如何使用,包括使用memwatch.c 监测内存时需要定义#define new  wmNew  #define delete  wmDelete,需要包含头文件memwatch.h等等

       readme 如何编译,链接自己的代码,生成可执行程序 ( 默认生成a.out ) 监测内存

       memwatch.c MW系列接口定义

       memwatch.h MW系列接口声明

       test.c  自带的测试程序,可以换成自己的代码


3. 如何得到memwatch.log

    解包之后,进入目录memwatch-2.71,直接make即可生成 a.out ,执行a.out,最后当前目录下会生成memwatch.log.


4. 注意事项

    (1)手动编译时,添加宏定义$(CC) -DMEMWATCH -DMW_STDIO test.c memwatch.c

    (2)测试程序,添加头文件memwatch.h

    (3)定义宏

              #define new  wmNew  

              #define delete  wmDelete


5. 监测日志查看,分析

      

     上图显示了,在test.c 文件申请了但没有释放的内存的地方line 59 .

      参照代码比对      
      
[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /* 
  2. **  NOTE: Running this program in a Win32 or Unix environment 
  3. **  will probably result in a segmentation fault or protection 
  4. **  error. These errors may be caused by MEMWATCH when it is 
  5. **  looking at memory to see if it owns it, or may be caused by 
  6. **  the test program writing to memory it does not own. 
  7. ** 
  8. **  MEMWATCH has two functions called 'mwIsReadAddr()' and 
  9. **  'mwIsSafeAddr()', which are system-specific. 
  10. **  If they are implemented for your system, and works 
  11. **  correctly, MEMWATCH will identify garbage pointers and 
  12. **  avoid causing segmentation faults, GP's etc. 
  13. ** 
  14. **  If they are NOT implemented, count on getting the core 
  15. **  dumped when running this test program! As of this writing, 
  16. **  the safe-address checking has been implemented for Win32 
  17. **  and ANSI-C compliant systems. The ANSI-C checking traps 
  18. **  SIGSEGV and uses setjmp/longjmp to resume processing. 
  19. ** 
  20. **  Note for Win95 users: The Win32 IsBadReadPtr() and its 
  21. **  similar functions can return incorrect values. This has 
  22. **  not happened under WinNT, though, just Win95. 
  23. ** 
  24. **  991009 Johan Lindh 
  25. ** 
  26. */  
  27.   
  28. #include <stdio.h>  
  29. #include <signal.h>  
  30. #include "memwatch.h"  
  31.   
  32. #ifndef SIGSEGV  
  33. #error "SIGNAL.H does not define SIGSEGV; running this program WILL cause a core dump/crash!"  
  34. #endif  
  35.   
  36. #ifndef MEMWATCH  
  37. #error "You really, really don't want to run this without memwatch. Trust me."  
  38. #endif  
  39.   
  40. #if !defined(MW_STDIO) && !defined(MEMWATCH_STDIO)  
  41. #error "Define MW_STDIO and try again, please."  
  42. #endif  
  43.   
  44.   
  45. int main()  
  46. {  
  47.     char *p;  
  48.   
  49.     /* Collect stats on a line number basis */  
  50.     mwStatistics( 2 );  
  51.   
  52.     /* Slows things down, but OK for this test prg */  
  53.     /* mwAutoCheck( 1 ); */  
  54.   
  55.     TRACE("Hello world!\n");  
  56.   
  57.     p = malloc(210);  
  58.     free(p);  
  59.     p = malloc(20);  
  60.     p = malloc(200);    /* causes unfreed error */  
  61.     p[-1] = 0;          /* causes underflow error */  
  62.     free(p);  
  63.   
  64.     p = malloc(100);  
  65.     p[ -(int)(sizeof(long)*8) ] = -1; /* try to damage MW's heap chain */  
  66.     free( p ); /* should cause relink */  
  67.   
  68.     mwSetAriFunc( mwAriHandler );  
  69.     ASSERT(1==2);  
  70.   
  71.     mwLimit(1000000);  
  72.     mwNoMansLand( MW_NML_ALL );  
  73.   
  74.     ......  
  75.       
  76.     return 0;  
  77. }  


    发现在line 57,59,60,64四个地方分别申请了内存,只有第59行申请的内存没有释放!刚好和log unfreed对应的行数一样。

6. memwatch 扩展用法
    详细用法可参见memwatch.h,也有一篇不错的 文章

更多内存泄露工具,后续研究!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值