Home > Article > System Tutorial > How to detect and solve memory leaks under Linux
Memory leak refers to the phenomenon that the program applies for memory space but does not release it in time during the running process, causing more and more memory to be occupied, and even causing the system to crash. Memory leaks are a common software defect and are a problem that cannot be ignored for Linux systems. So, how to find and fix memory leaks under Linux? What tools can help us detect and analyze memory leaks? This article will introduce you to several commonly used memory leak tools under Linux, allowing you to better manage and optimize memory resources under Linux.
Memory leaks can be divided into the following categories:
1. Frequent memory leaks. Code with memory leaks will be executed multiple times, causing a memory leak every time it is executed.
2. Sporadic memory leaks. Code that causes memory leaks will only occur under certain circumstances or operations. Frequent and sporadic are relative. For certain circumstances, what is occasional may become common. So the testing environment and testing methods are crucial to detecting memory leaks.
3. One-time memory leak. The code that causes a memory leak will only be executed once, or due to algorithmic flaws, there will always be one and only one block of memory leaked. For example, memory is allocated in the constructor of a Singleton class, but the memory is not released in the destructor. There is only one instance of the Singleton class, so the memory leak will only occur once.
4. Implicit memory leak. The program continuously allocates memory while it is running, but does not release the memory until the end. Strictly speaking, there is no memory leak here, because the program eventually releases all the requested memory. But for a server program that needs to run for days, weeks or even months, failure to release memory in time may also lead to the eventual exhaustion of all the system's memory. Therefore, we call this type of memory leak an implicit memory leak.
There are many ways to detect memory leaks. The following lists the commonly used memory leak detection tools in Linux.
Application environment: Linux GLIBC
Programming language: C
Usage: Include the header file mcheck.h, define the environment variable MALLOC_TRACE as the output file name, and call mtrace() when the program starts.
Result output: user-specified file
Design idea: Add hook functions for malloc, realloc, and free functions, and record the execution of each malloc-free pair
Advantages and disadvantages: Only memory leaks caused by using malloc/realloc/free can be checked
How to obtain: GLIBC comes with it and can be used directly
Application environment: Linux
Programming language: C
Usage: Add memwatch.h, add -DMEMWATCH -DMW_STDIO and memwatch.c
when compilingResult output: The output file name is memwatch.log. During the execution of the program, error prompts will be displayed on stdout
Design idea: Redefine malloc/realloc/calloc/strdup/free, etc. as mwMalloc(sz, FILE, LINE), etc., and maintain an operation linked list internally
Advantages and disadvantages: Can detect double-free, erroneous free, unfreed memory, overflow, underflow, etc.
How to obtain: http://memwatch.sourceforge.net/
Application environment: Linux
Programming language: C/C
Usage: Add the -g option when compiling, such as gcc -g filename.c -o filename, and use the following command to detect memory usage:
Result output: #valgrind –tool=memcheck –leak-check=yes –show-reachable=yes ./filename, you will see the memory usage report
Design idea: Maintain a valid address space table and an invalid address space table (address space of the process) according to the memory operation of the software
Advantages and Disadvantages: Able to detect:
How to obtain: http://valgrind.org/
Application environment: Linux/Windows
Programming language: C
Usage: Include header file debug_new.h, link debug_new.cpp
Result output: console console
Design idea: Capture memory application/release requests by overloading the new and delete operators, and maintain a hash list of global static variables inside the program. In the new operator, it not only allocates the memory requested by the user, but also adds a header to each allocated memory, which stores the location information and linked list pointer of the allocation. New returns the allocated block. The value after adding the head offset to the memory, and this return value has been HASH calculated before and added to the HASH linked list. When deleting, first perform HASH calculation based on the pointer address to be released, and then traverse the linked list at the array HASH value to search. If found, the node will be removed. If not found, it will be aborted. In this way, after the program ends, we can determine whether there is a memory leak by checking whether there are any unreleased memory blocks in this array.
Advantages and disadvantages: cross-platform, only used for C programs,
How to obtain: http://www.ibm.com/developerworks/cn/linux/l-mleak2/index.html
The methods used by the above analysis tools can be roughly divided into the following categories:
1. Register memory allocation/release hook function (hook). Under Linux, there are five hook functions such as malloc_hook and free_hook. Under Windows, you can register the _CrtSetAllocHook hook function, so that this request can be captured and processed when memory is allocated. Visual Leak Detecter and mtrace use this method.
2. Use macro definition replacement. Replace malloc and free in user code with custom functions such as macro-defined mwMalloc(sz, FILE, LINE) to track memory requests. Memwatch uses this method.
3. Operator overloading. This method is only used in C language. It implements tracking memory requests by overloading the new and delete operators. The overloaded operators are similar to the meaning of hook functions. debug_new takes this approach.
The output methods of these tools are also divided into the following types:
1. Under normal circumstances, the output is generally output to the debugging window. Many software itself provides an ideal output place, and the output of GUI applications to the standard output is not visible. Visual Leak Detecter uses this method.
2. Output to standard output or standard error output: Console applications can output to the screen, such as memwatch, valgrind, and debug_new all use this method.
3. Output to log file: Output the results to user-specified or default log files, such as mtrace and memwatch.
In addition, the memory detection methods of these tools are divided into two types:
1. Maintain a memory operation linked list. When there is a memory application operation, it is added to this linked list. When there is a release operation, it is removed from the linked list from the application operation. If there is still content in the linked list after the program ends, it means that there is a memory leak; if the memory operation to be released does not find the corresponding operation in the linked list, it means that it has been released multiple times. Use this method with built-in debugging tools, Visual Leak Detecter, mtrace, memwatch, debug_new.
2. Simulate the address space of the process. Following the operating system's handling of process memory operations, an address space mapping is maintained in user mode. This method requires a deep understanding of the processing of process address spaces. Because the process address space distribution of Windows is not open source, it is difficult to simulate, so it is only supported on Linux. The one that takes this approach is valgrind.
Through this article, you should have a basic understanding of the memory leak problem under Linux and know its causes, effects and solutions. You also learned about several commonly used memory leak tools under Linux, such as Valgrind, Memwatch, Mtrace, etc., as well as their usage methods, advantages and disadvantages. We recommend that you use these tools to detect and analyze memory leaks when developing and testing Linux programs to improve program performance and stability. At the same time, we also remind you to pay attention to some precautions and restrictions when using these tools to avoid misjudgments or omissions. I hope this article can help you better use the Linux system and enable you to write high-quality programs under Linux.
The above is the detailed content of How to detect and solve memory leaks under Linux. For more information, please follow other related articles on the PHP Chinese website!