简介:
Dmalloc检测内存泄露工具。
安装:
① 下载
http://dmalloc.com/releases/
② 安装
tar zxvf dmalloc-5.5.2.tgz
cd dmalloc-5.5.2
./configure --prefix=/usr
make
make install
使用:
① 环境变量设置
/root/.bashrc文件最后一行追加function dmalloc { eval `command dmalloc -b $*`; }
② 退出root,重新登陆root
③ 编译时,需追加DMALLOC和DMALLOC_FUNC_CHECK
④ 链接 libdmalloc.a库
⑤ 运行前,需运行命令dmalloc -l logfile -i 100 low
实例:
① 手顺
gcc -DDMALLOC -DDMALLOC_FUNC_CHECK -ldmalloc test.c
dmalloc -l logfile -i 100 low
./test
② 代码
1 //gcc -DDMALLOC -DDMALLOC_FUNC_CHECK -ldmalloc test.c 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <string.h> 5 #ifdef DMALLOC 6 #include <dmalloc.h> 7 #endif 8 int main(int argc, char **argv) 9 { 10 char *string; 11 string = (char*)malloc(sizeof(char)); 12 string = (char*)malloc(sizeof(int*)); 13 14 return 0; 15 }
③ 解析结果
1378340744: 2: Dmalloc version '5.5.1' from 'http://dmalloc.com/'
1378340744: 2: flags = 0x4e48503, logfile 'logfile'
1378340744: 2: interval = 100, addr = 0, seen # = 0, limit = 0
1378340744: 2: threads enabled, lock-on = 0, lock-init = 2
1378340744: 2: starting time = 1378340744
1378340744: 2: process pid = 27089
1378340744: 2: Dumping Chunk Statistics:
1378340744: 2: basic-block 4096 bytes, alignment 8 bytes
1378340744: 2: heap address range: 0x110000 to 0xd74000, 12992512 bytes
1378340744: 2: user blocks: 1 blocks, 4069 bytes (9%)
1378340744: 2: admin blocks: 9 blocks, 36864 bytes (90%)
1378340744: 2: total blocks: 10 blocks, 40960 bytes
1378340744: 2: heap checked 1
1378340744: 2: alloc calls: malloc 2, calloc 0, realloc 0, free 0
1378340744: 2: alloc calls: recalloc 0, memalign 0, valloc 0
1378340744: 2: alloc calls: new 0, delete 0
1378340744: 2: current memory in use: 5 bytes (2 pnts)
1378340744: 2: total memory allocated: 5 bytes (2 pnts)
1378340744: 2: max in use at one time: 5 bytes (2 pnts)
1378340744: 2: max alloced with 1 call: 4 bytes
1378340744: 2: max unused memory space: 27 bytes (84%)
1378340744: 2: top 10 allocations:
1378340744: 2: total-size count in-use-size count source
1378340745: 2: 4 1 4 1 test.c:12
1378340745: 2: 1 1 1 1 test.c:11
1378340745: 2: 5 2 5 2 Total of 2
1378340745: 2: Dumping Not-Freed Pointers Changed Since Start:
1378340745: 2: not freed: '0x797fe8|s1' (4 bytes) from 'test.c:12'
1378340745: 2: not freed: '0x797ff8|s1' (1 bytes) from 'test.c:11'
1378340745: 2: total-size count source
1378340745: 2: 4 1 test.c:12
1378340745: 2: 1 1 test.c:11
1378340745: 2: 5 2 Total of 2
1378340745: 2: ending time = 1378340745, elapsed since start = 0:00:01