Dmalloc Usage Quick Start

Source: Internet
Author: User

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

    1. Tar zxvf dmalloc-5.5.2.tgz
    2. CD dmalloc-5.5.2
    3. ./configure
    4. Make
    5. 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:

    1. 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

  1. 1214894489:2: Dmalloc version ' 5.5.2 ' from ' http://dmalloc.com/'
  2. 1214894489:2: Flags = 0x4e48503, logfile ' logfile '
  3. 1214894489:2: Interval = +, 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 Ten 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: 2 Each 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: 2 Total of 2
  33. 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 =============

    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. }

Dmalloc Usage Quick Start

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.