Linux memory leak detection techniques

In C and C + + programs, it's up to the programmer to apply for and release memory independently. If he doesn't pay attention, he will import memory errors into the system. At the same time, memory errors are often very serious, which will bring serious consequences such as system crash and memory exhaustion. Whenever and wherever a memory leak occurs, it may be completely unacceptable to the application, and the memory leak is not obvious [1]. C and C + + programs with memory errors can cause various problems. If they leak memory, they will slow down and eventually stop running; If the memory is covered, it will become very fragile and vulnerable to malicious users.

download-icon
Free Download
for VM, OS, DB, File, NAS, etc.
nick-zhao

Updated by Nick Zhao on 2021/06/16

-bash: mtrace: command not found

The reason:

The system is missing glibc-utils

Centos: sudo yum install glibc-utils

Because C and C++ programs are entirely up to the programmer to apply and free memory, a little careless, will import memory errors into the system. At the same time, memory errors are often very serious, generally bring such as system crash, memory run out of such serious consequences. Whenever and wherever a memory leak occurs, it can appear to be completely unacceptable to the application and it is not obvious [1]. C and C++ programs with memory errors can cause a variety of problems. If they leak memory, they slowly slow down and eventually stop running. If you overwrite memory, it becomes very fragile and vulnerable to abuse by malicious users.

What is a memory leak

A memory leak generally refers to a leak of heap memory. Heap memory is the amount of memory that a program allocates from the heap to an arbitrary size (the size of the memory block can be determined during the program's run time) and must display when it is used up. An application typically allocates a block of memory from the heap using functions such as malloc, realloc, new, etc. When it is done, the program must be responsible for freeing the block with a corresponding call to free or delete. Otherwise, the block of memory cannot be used again, and we say the block of memory is leaking.

How do I find a memory leak

Some simple memory leaks can be identified during the inspection phase of the code. Some of the more serious leaks that cause a program or system to crash in a very short period of time, or the system reports not having enough memory, are also relatively easy to find. The hardest part is that the leaks are slow, requiring days, weeks or even months of observation before any significant anomalies can be seen. So how to detect a potential memory leak in a relatively short time? In fact, different systems have memory monitoring tools, from which we can collect stack memory information over a period of time, observe the growth trend, to determine whether there is a memory leak. On Linux, you can use the ps command to monitor memory usage, such as the following command (observe the VSZ value of a given process) :

Linux memory leak detection tool (1) : mtrace

MTrace is the simplest of the three tools. MTrace is a C function that is declared and defined in the function prototype:

void mtrace(void);

In fact, mtrace is a malloc handler similar to malloc_hook, but the handler function of mtrace is already written by the system for you, but in this case, how does the system know where you want to write the malloc/free record? To do this, set the MALLOC_TRACE environment variable before calling mtrace() :

#include

.

setenv("MALLOC_TRACE", "output_file_name", 1);

.

Output_file_name is the name of the file in which the test results are stored.

However, the format of the results is incomprehensible to the general public, and whenever MTrace is installed, there is a Perl script called MTrace, and the following command is typed in the shell:

mtrace [binary] output_file_name

This translates the contents of the output_file_name into statements that can be understood, such as "No memory leaks", "0x12345678 Free 10 was never alloc" and so on.

For example, there is a function :(set aside the principle of single entry and single exit for a moment)

#include

#include

#include

#include

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;

}

After execution, use mtrace to print the result:

0x08049670 Free 3 was never alloc'd 0x42029acc

0x080496f0 Free 4 was never alloc'd 0x420dc9e9

0x08049708 Free 5 was never alloc'd 0x420dc9f1

0x08049628 Free 6 was never alloc'd 0x42113a22

0x08049640 Free 7 was never alloc'd 0x42113a52

0x08049658 Free 8 was never alloc'd 0x42113a96

Memory not freed:

Address Size Caller

0x08049a90 0x1 at 0x80483fe

The last line indicates that there is an unfreed memory of 1 byte, probably "hello".

If we free this memory segment:

#include

#include

#include

#include

int main() {

char *hello;

setenv("MALLOC_TRACE", "output", 1);

mtrace();

if ((hello = (char *) malloc(sizeof(char))) == NULL) {

perror("Cannot allocate memory.");

return -1;

}

free(hello);

return 0;

}

The results are as follows:

0x080496b0 Free 4 was never alloc'd 0x42029acc

0x08049730 Free 5 was never alloc'd 0x420dc9e9

0x08049748 Free 6 was never alloc'd 0x420dc9f1

0x08049668 Free 7 was never alloc'd 0x42113a22

0x08049680 Free 8 was never alloc'd 0x42113a52

0x08049698 Free 9 was never alloc'd 0x42113a96

No memory leaks.

The principle of MTrace is to record the execution of each pair of malloc free. If each malloc has corresponding free, it means that there is no memory leak. For any memory leak that is not malloc/free, MTrace cannot find out the problem.

Linux memory leak detection tool (2) : memwatch

Of the three detection tools, MemWatch is the easiest to set up. Like dmalloc, it can detect unfreed memory, multiple releases of the same memory segment, incorrect access to addresses, and improper use of unallocated memory areas.

How to download:

Download address 1: http://www.hnlyyk.icoc.cc/col.jsp?id=103 click open the link The latest version memwatch - 2.71. Tar. Gz

Download address 2: http://www.linkdata.se/downloads/sourcecode/memwatch/memwatch-2.71.tar.gz

Fortunately, MemWatch does not need to be installed at all, because it is just a set of C program code. Just add MemWatch. H to your program and add -dMemWatch-dmw_stdio and MemWatch. C at compile time to use MemWatch, for example:

gcc -DMEMWATCH -DMW_STDIO test.c memwatch.c -o test

MemWatch outputs the results

The output filename of memwatch is called memwatch.log, and during the execution of the program, all error messages will be displayed on stdout. If memwatch fails to write to the above file, it will try to write memwatchnn.log, where NN is between 01 and 99, If it still fails to write to memwatchnn.log, the write to the file is abandoned.

We refer to the problematic code we used in the first article (MTrace) :

#include

#include

#include

#include

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 compile instructions in the shell:

gcc -DMEMWATCH -DMW_STDIO test.c memwatch.c -o test

The contents of memwatch.log are as follows:

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

Started at Sat Jun 26 22:48:47 2004

Modes: __STDC__ 32-bit mwDWORD==(unsigned long)

mwROUNDALLOC==4 sizeof(mwData)==32 mwDataSize==32

Stopped at Sat Jun 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 states that when test.c was executed on line 9, the allocated memory was still not freed. The size of the memory segment was 1 byte.

Be careful with Memwatch

The advantage of MemWatch is that it does not require special configuration and can be used without installation, but the disadvantage is that it can slow down the program, especially when it does a lot of checking to free memory. However, it has one more feature than MTrace and dmalloc. It can simulate a system running out of memory. Users can limit the heap memory size (in bytes) by using the mwLimit(long num_of_byte) function.

Share on:

Categories: Tech Tips