Linux C Programming Memory Leak Detection Tool (ii): Memwatch

Source: Internet
Author: User

Memwatch Brief Introduction

Among the three types of detection tools, the simplest is the memwatch, which, like Dmalloc, detects memory that is not released, multiple releases of the same memory, access errors, and improper use of unallocated memory areas. Please go to http://www.linkdata.se/sourcecode.html to download the latest version number of Memwatch.

Installation and use of Memwatch

Fortunately, Memwatch does not need to be installed at all, because it is just a set of C program code, only to add to your program, MEMWATCH.H, compile-time plus-dmemwatch-dmw_ Stdio and MEMWATCH.C can use Memwatch, such as:

Gcc-dmemwatch-dmw_stdio test.c memwatch.c-o Test

Memwatch Output Results

Memwatch output file name is Memwatch.log, and during the program run, all error prompts will be displayed on stdout, assuming Memwatch failed to write to the above file, it will try to write MemwatchNN.log, and nn between 01 to 99 if it still fails to write MEMW AtchNN.log, the write file is discarded.

We quoted the problematic code used in the first article (mtrace):

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <memwatch.h>
int main () {
Char *hello;

Setenv ("Malloc_trace", "Output", 1);
Mtrace ();
if ((hello = (char *) malloc (sizeof (char))) = = = NULL) {
Perror ("Cannot allocate memory.");
return-1;
}

return 0;
}

then enter the following compilation directives in the shell:

Gcc-dmemwatch-dmw_stdio test.c memwatch.c-o Test

The contents of Memwatch.log such as the following:

============= memwatch 2.71 Copyright (C) 1992-1999 Johan Lindh =============

Started at Sat June 26 22:48:47 2004

Modes: __stdc__ 32-bit mwdword== (unsigned long)
mwroundalloc==4 sizeof (mwdata) ==32 mwdatasize==32


Stopped at Sat June 26 22:48:47 2004

Unfreed: <1> test.c (9), 1 bytes at 0x805108c {FE ...................}

Memory usage Statistics (GLOBAL):
N) Umber of Allocations made:1
L) argest Memory usage:1
T) Otal of All alloc () calls:1
U) nfreed bytes totals:1

The file indicates that the memory allocated when TEST.C is run to the 9th row is still not released, and the memory size is 1 byte.

Memwatch Use note

The advantage of Memwatch is that it doesn't need to be configured and can be used without installation, but the downside is that it slows down the execution of the program, especially when it frees up memory for a lot of checking. But it is more than mtrace and dmalloc a function, is to simulate the system memory shortage situation, the user only need to use the Mwlimit (long num_of_byte) function to limit the size of the heap memory (in bytes).

The most specific instructions (including strengths and weaknesses, implementation principles, etc.) have been listed in the Readme, I strongly recommend that you read the document.

RELATED links:

-Memwatch Download

Linux C Programming Memory Leak Detection Tool (ii): Memwatch

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.