Original link
Common memory leak detection methods are
- 1 mtrace
- 2 Memwatch
- 3 Mpatrol
- 4 Dmalloc
- 5 Dbgmem
- 6 Valgrind
- 7 Electric Fence
Dmalloc is a simple and easy-to-use leak Check tool that is published as a runtime library.
Dmalloc is able to check out memory that has not been released until the end of the program, and can pinpoint
The first line of the source file.
Dmalloc Home: http://dmalloc.com
Supported platforms: AIX, Bsd/os, Dg/ux, Free/net/openbsd, Gnu/hurd, HPUX, Irix, Linux, Ms-dog, NeXT, OSF, SCO, Solaris, SunOS, Ultrix, U Nixware, Windoze, and even Unicos on a Cray t3e
Latest Version: 5.5.2
Installation: Download http://dmalloc.com/releases/dmalloc-5.5.2.tgz
- Tar zxvf dmalloc-5.5.2.tgz
- CD dmalloc-5.5.2
- ./configure
- Make
- Make install
Set Environment variables:
For Bash, Ksh, and Zsh (http://www.zsh.org/), in '. BASHRC ', '. Profile ', or '. ZSHRC '
A row is added to the file (the-b parameter indicates the output for bash):
function dmalloc {eval ' command dmalloc-b $* ';}
Then log back on to the user, or execute: source ~/.BASHRC or Source ~/.profile
Execute the following:
- Dmalloc-l Logfile-i
Add the following C code to the source file:
#ifdef DMALLOC
#include "dmalloc.h"
#endif
Compiled using cflags:-ddmalloc-ddmalloc_func_check
such as: Gcc-ddmalloc-ddmalloc_func_check dm_test.c
Perform:
./a.out
Running the above command will generate the logfile file in the current directory and see the contents of the logfile as follows:
Cat logfile
- 1214894489:2: Dmalloc version ' 5.5.2 ' from ' http://dmalloc.com/'
- 1214894489:2: Flags = 0x4e48503, logfile ' logfile '
- 1214894489:2: Interval = +, 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 Ten 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: 2 Each 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: 2 Total of 2
- 1214894489:2: Ending time = 1214894489, elapsed since start = 0:00:00
So, where the memory leak is at a glance.
====== dm_test.c Source Code =============
- #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;
- }
Dmalloc Usage Quick Start