dmalloc的使用

dmalloc到/usr/local/bin/目录下。 
(6) 设置环境变量:
对于 Bash, ksh, and zsh (http://www.zsh.org/), 在 `.bashrc', `.profile', or `.zshrc'
文件中加入一行 ( -b 参数表示针对Bash的输出):

function dmalloc { eval `/usr/local/bin/dmalloc -b $*`; }  

然后重新登陆用户,或者执行: source ~/.bashrc 或 source ~/.profile

接下面执行:

dmalloc -l logfile -i 100 low

或者

export DMALLOC_OPTIONS=debug=0x4e48503,inter=100,log=./mydmalloc.log

dmalloc -l logfile -i 100 low

Set the malloc logfile name to ‘logfile’ (-l logfile).

Have the library check itself every 100 iterations (-i 100). This controls how fast your program will run. Larger numbers check the heap less and so it will run faster. Lower numbers will be more likely to catch memory problems.

Enable a number of debug features (low). You can also try runtime for minimal checking or medium or high for more extensive heap verification.

By default, the low, medium, and high values enable the error-abort token which will cause the library to abort and usually dump core immediately upon seeing an error. See Section 3.4 [Dumping Core], page 16. You can disable this feature by entering dmalloc -m error-abort (-m for minus) to remove the error-abort token and your program will just log errors and continue.


4. usage 针对需要使用dmalloc的源代码作如下修改:
(1) 在源代码中,添加下面的C代码:
#ifdef DMALLOC
#include "dmalloc.h"
#endif
(2) 在Makefile中,添加 -DDMALLOC   CFLAGS -DDMALLOC_FUNC_CHECK 
如: gcc -DDMALLOC -DDMALLOC_FUNC_CHECK dm_hello.c -ldmalloc -o dm_hello

cc -DDMALLOC -DDMALLOC_FUNC_CHECK dm_hello.c   -ldmalloc -o dm_hello

cc -DDMALLOC -DDMALLOC_FUNC_CHECK -ggdb -Wall -ldmalloc  dm_hello.c  -o dm_hello 
(3) 添加-ldmalloc选项 运行之后,可以在/home/leo/mydmalloc.log中查看检测信息。如果不使用绝对路径,则logfile会生成在app所在的目录。



5. dmalloc in Embedded Linux 

6. advantage & disadvantage advantage: 能检测未释放的内存、同一段内存被释放多次、位址存取错误及不当使用未分配之内存区域。 disadvantage: 只检测堆上内存,对栈内存和静态内存无能为力。 只用于利用malloc申请的内存,而对使用sbrk()或mmap()分配的内存无能为力。 不能用于读写没有申请或没有初始化的内存、写只读内存。 


7. Reference
[1] dmalloc使用. http://www.diybl.com/course/6_system/linux/Linuxjs/2008827/137963.html 
[2] 使用Dmalloc检测内存泄露的范围和局限性. http://www.sudu.cn/info/html/edu/20070101/290494.html 
[3] dmalloc在嵌入式的开发板上的应用.http://blog.sina.com.cn/s/blog_613480a50100ekc0.html

例子:

运行上面的命令会在当前目录下生成 logfile文件,查看logfile的内容如下:

cat logfile

  1. 1214894489: 2: Dmalloc version '5.5.2' from 'http://dmalloc.com/'
  2. 1214894489: 2: flags = 0x4e48503, logfile 'logfile'
  3. 1214894489: 2: interval = 100, addr = 0, seen # = 0, limit = 0
  4. 1214894489: 2: starting time = 1214894489
  5. 1214894489: 2: process pid = 9560
  6. 1214894489: 2: Dumping Chunk Statistics:
  7. 1214894489: 2: basic-block 4096 bytes, alignment 8 bytes
  8. 1214894489: 2: heap address range: 0xb8020000 to 0xb8029000, 36864 bytes
  9. 1214894489: 2: user blocks: 1 blocks, 4043 bytes (10%)
  10. 1214894489: 2: admin blocks: 8 blocks, 32768 bytes (89%)
  11. 1214894489: 2: total blocks: 9 blocks, 36864 bytes
  12. 1214894489: 2: heap checked 1
  13. 1214894489: 2: alloc calls: malloc 2, calloc 0, realloc 0, free 0
  14. 1214894489: 2: alloc calls: recalloc 0, memalign 0, valloc 0
  15. 1214894489: 2: alloc calls: new 0, delete 0
  16. 1214894489: 2: current memory in use: 11 bytes (2 pnts)
  17. 1214894489: 2: total memory allocated: 11 bytes (2 pnts)
  18. 1214894489: 2: max in use at one time: 11 bytes (2 pnts)
  19. 1214894489: 2: max alloced with 1 call: 6 bytes
  20. 1214894489: 2: max unused memory space: 53 bytes (82%)
  21. 1214894489: 2: top 10 allocations:
  22. 1214894489: 2: total-size count in-use-size count source
  23. 1214894489: 2: 6 1 6 1 dm_test.c:71
  24. 1214894489: 2: 5 1 5 1 dm_test.c:69
  25. 1214894489: 2: 11 2 11 2 Total of 2
  26. 1214894489: 2: Dumping Not-Freed Pointers Changed Since Start:
  27. 1214894489: 2: not freed: '0xb8028fc8|s1' (6 bytes) from 'dm_test.c:71'
  28. 1214894489: 2: not freed: '0xb8028fe8|s1' (5 bytes) from 'dm_test.c:69'
  29. 1214894489: 2: total-size count source
  30. 1214894489: 2: 6 1 dm_test.c:71
  31. 1214894489: 2: 5 1 dm_test.c:69
  32. 1214894489: 2: 11 2 Total of 2
  33. 1214894489: 2: ending time = 1214894489, elapsed since start = 0:00:00

那么,哪个地方的内存leak就一目了然了。

//====== dm_test.c 源代码 =============

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #ifdef DMALLOC
  5. #include <dmalloc.h>
  6. #endif
  7. int main(int argc, char **argv)
  8. {
  9. char *str;
  10. str = malloc(5);
  11. str = malloc(6);
  12. return 0;
  13. }

    简介:
    这个工具可以用来检测堆内存的泄露或溢出问题,支持多线程分配,支持64MB内存.
    xReader使用它发现了大量内存问题.

    使用方法:
    在Makefile里加入-ldmalloc
    检查内存泄露,在程序启动后调用:

    1. dmalloc_debug_setup("log-stats,log-non-free,check-fence,check-funcs,check-blank,print-messages");
    复制代码

    在源代码中加入:

    1. mark = dmalloc_mark();
    2. // 这里是你要测试的可能泄露内存的操作
    3. // ....
    4. dmalloc_log_changed(mark, 1, 0, 1); // 打印堆内存自从dmalloc_mark后的分配释放信息
    5. dmalloc_log_stats(); // 打印堆内存的全局信息.
    复制代码

    如果检查内存溢出,将dmalloc_debug_setup改为

    1. dmalloc_debug_setup("log-stats,log-non-free,check-fence,check-heap,check-funcs,check-blank,print-messages,inter=100");
    复制代码

    这样内存分配释放后将检查整个堆的正确性,速度将只有原来的1/20,但可以检查出堆溢出问题.

    dmalloc的输出会显示在PSPLINK的PC端,如果编译程序时用-O0 -g,dmalloc还能显示出错误的堆内存操作的源代码行号.

    如果同gdb一起使用,可对dmalloc_error下断点,一旦程序中断用bt指令就知道程序溢出的上下文了.

    详细解释请看dmalloc自带的文档,还有xReader源代码.

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值