dmalloc的使用
来源:互联网 发布:万宝瑞华人才管理 知乎 编辑:程序博客网 时间:2024/05/21 22:22
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
- 1214894489: 2: Dmalloc version '5.5.2' from 'http://dmalloc.com/'
- 1214894489: 2: flags = 0x4e48503, logfile 'logfile'
- 1214894489: 2: interval = 100, addr = 0, seen # = 0, limit = 0
- 1214894489: 2: starting time = 1214894489
- 1214894489: 2: process pid = 9560
- 1214894489: 2: Dumping Chunk Statistics:
- 1214894489: 2: basic-block 4096 bytes, alignment 8 bytes
- 1214894489: 2: heap address range: 0xb8020000 to 0xb8029000, 36864 bytes
- 1214894489: 2: user blocks: 1 blocks, 4043 bytes (10%)
- 1214894489: 2: admin blocks: 8 blocks, 32768 bytes (89%)
- 1214894489: 2: total blocks: 9 blocks, 36864 bytes
- 1214894489: 2: heap checked 1
- 1214894489: 2: alloc calls: malloc 2, calloc 0, realloc 0, free 0
- 1214894489: 2: alloc calls: recalloc 0, memalign 0, valloc 0
- 1214894489: 2: alloc calls: new 0, delete 0
- 1214894489: 2: current memory in use: 11 bytes (2 pnts)
- 1214894489: 2: total memory allocated: 11 bytes (2 pnts)
- 1214894489: 2: max in use at one time: 11 bytes (2 pnts)
- 1214894489: 2: max alloced with 1 call: 6 bytes
- 1214894489: 2: max unused memory space: 53 bytes (82%)
- 1214894489: 2: top 10 allocations:
- 1214894489: 2: total-size count in-use-size count source
- 1214894489: 2: 6 1 6 1 dm_test.c:71
- 1214894489: 2: 5 1 5 1 dm_test.c:69
- 1214894489: 2: 11 2 11 2 Total of 2
- 1214894489: 2: Dumping Not-Freed Pointers Changed Since Start:
- 1214894489: 2: not freed: '0xb8028fc8|s1' (6 bytes) from 'dm_test.c:71'
- 1214894489: 2: not freed: '0xb8028fe8|s1' (5 bytes) from 'dm_test.c:69'
- 1214894489: 2: total-size count source
- 1214894489: 2: 6 1 dm_test.c:71
- 1214894489: 2: 5 1 dm_test.c:69
- 1214894489: 2: 11 2 Total of 2
- 1214894489: 2: ending time = 1214894489, elapsed since start = 0:00:00
那么,哪个地方的内存leak就一目了然了。
//====== dm_test.c 源代码 =============
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #ifdef DMALLOC
- #include <dmalloc.h>
- #endif
- int main(int argc, char **argv)
- {
- char *str;
- str = malloc(5);
- str = malloc(6);
- return 0;
}
简介:
这个工具可以用来检测堆内存的泄露或溢出问题,支持多线程分配,支持64MB内存.
xReader使用它发现了大量内存问题.
使用方法:
在Makefile里加入-ldmalloc
检查内存泄露,在程序启动后调用:- dmalloc_debug_setup("log-stats,log-non-free,check-fence,check-funcs,check-blank,print-messages");
在源代码中加入:
- mark = dmalloc_mark();
- // 这里是你要测试的可能泄露内存的操作
- // ....
- dmalloc_log_changed(mark, 1, 0, 1); // 打印堆内存自从dmalloc_mark后的分配释放信息
- dmalloc_log_stats(); // 打印堆内存的全局信息.
如果检查内存溢出,将dmalloc_debug_setup改为
- 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源代码.
- dmalloc的使用
- dmalloc的使用
- dmalloc使用
- Solaris上的Dmalloc
- 使用dmalloc&valgrind检测内存泄露
- broadcom63283环境使用dmalloc调试内存泄露
- dmalloc在嵌入式的开发板上的应用
- dmalloc在嵌入式的开发板上的应用
- dmalloc检测MLK
- How to use dmalloc
- dmalloc用法快速入门
- dmalloc检测程序内存泄漏
- 如何用dmalloc检测内存泄漏(原创)
- dmalloc 原文 翻译整理(1)
- dmalloc 原文 翻译整理(2)
- dmalloc 原文 翻译整理(3)
- dmalloc 原文 翻译整理(4)
- linux 下dmalloc安装及环境配置
- 在PhpStorm中配置XDebug调试PHP代码
- Dialog去除边框代码
- uml of linux
- 最长递增子序列 O(n^2) 与 O(nlogn)
- 编写一个函数 char *my_strnchr(char const *str,int ch,int which)
- dmalloc的使用
- 用笔记本远程访问连接在路由器上的树莓派
- Javascript:表单验证(验证空值/邮箱格式)
- JAR文件
- POJ 2385 Apple Catching
- C中如何调用C++函数
- iOS之KxMenu第三方的使用
- TypeError: $(...).ajaxSubmit is not a function
- Android4.4 Keyguard锁屏机制分析