On Linux memory Debugging Tools-memwatch

Source: Internet
Author: User

The C language as a standard programming language on Linux systems gives us great control over dynamic memory allocation. This freedom can lead to serious memory management problems that can cause a program to crash or cause performance degradation over time.

A memory leak (that is malloc() , an internal call that is free() never freed after execution) and a buffer overflow (for example, writes to memory previously allocated to an array) are common problems that may be difficult to detect. This section discusses several debugging tools that greatly simplify the process of detecting and locating memory problems

1. Memwatch Introduction
Memwatch is a C-language memory detector. In addition to the ability to detect memory, it can do other things as well, and we mainly lie about its basic functions. If you really want to know all the relevant details, you can view the header file memwatch.h and the source code in detail.

It uses the C-to-processor function, Memwatch replaces all memory allocation functions defined by ANSI C in your program with its own defined function function, and Memwatch's memory allocation function contains all the allocation information.

The Memwatch can be run on a cross-compiled target board.

2. Memwatch function
(1) Basically have basic memory management function
-mwmalloc ()
-mwfree ()
-mwcalloc ()
-mwrealloc ()
(2) features that Memwatch can detect include
-Double Release (Double-free),
-Error release (erroneous free),
-Memory leaks (unfreed memories),
-Overflow (Overflow),
-Underflow (underflow)

3. Compiling
When compiling, the Memwatch function, the compilation is added-dmemwatch


4. Memwatch.log Content
In normal operation, Memwatch will create a file called Memwatch.log. But sometimes, memwatch.log files cannot be created. At this point, Memwatch will attempt to create a file named similar to MemwatNN.log, where nn is a number between 01 and 99. If it fails, there is no log file.

5. Application examples

Listing 1. Memory Samples (TEST1.C)

#include <stdlib.h>
#include <stdio.h>
#include "memwatch.h"
int main (void)
{
Char *ptr1;
Char *ptr2;
PTR1 = malloc (512);
PTR2 = malloc (512);
PTR2 = PTR1;
Free (PTR2);
Free (PTR1);
}

The code in Listing 1 allocates two 512-byte blocks of memory, and then a pointer to the first memory block is set to point to the second block of memory. As a result, the address of the second memory block is lost, resulting in a memory leak.

Now we compile the memwatch.c of Listing 1. The following is an example of a makefile:

Test1

Gcc-dmemwatch-dmw_stdio test1.c Memwatch
C-o test1

When you run the Test1 program, it generates a report about the leaked memory. Listing 2 shows the sample Memwatch.log output file.

Listing 2. Test1 Memwatch.log File

  Memwatch 2.67 Copyright (C) 1992-1999 Johan Lindh
...
Double-free: <4> test1.c, 0x80517b4 was freed from test1.c (14)
...
Unfreed: <2> test1.c (one), bytes at 0x80519e4
{Fe fe-FE-FE fe fe fe fe fe fe fe fe ..... ...}
Memory usage Statistics (GLOBAL):
N) Umber of Allocations Made:2
L) argest Memory usage:1024
T) Otal of All alloc () calls:1024
U) nfreed bytes totals:512

Memwatch shows you the line that really caused the problem. If you release a pointer that has already been released, it will tell you. The same is true for memory that is not released. The end of the log displays statistics, including how much memory was leaked, how much memory was used, and how much memory was allocated in total.

On Linux memory Debugging Tools-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.