Overview
Memory leakage (Memory Leak) refers to the failure to release memory that is no longer in use due to negligence or errors. Memory leakage is a common problem in large and complex applications. When a previously allocated piece of memory is no longer needed or cannot be accessed, but it is not released, memory leakage occurs. Although excellent programming practices can ensure minimal leakage, it is very likely that memory leakage will occur when a large number of functions are used to process the same memory block.
Memory leakage can be divided into the following categories:
1. Frequent Memory leakage. Code with Memory leakage will be executed multiple times, resulting in a memory leak each time it is executed.
2. Occasional Memory leakage. Memory leakage occurs only in certain environments or operations. The frequency and frequency are relative. For a specific environment, unexpected events may become frequent. Therefore, the test environment and test method are crucial for detecting memory leaks.
3. One-time memory leakage. The code with Memory leakage is executed only once, or due to algorithm defects, there will always be one and only one piece of Memory leakage. For example, if the memory is allocated in a singleton constructor, the memory is not released in the destructor. The Singleton class only has one instance, So memory leakage only occurs once.
4. Implicit Memory leakage. The program continuously allocates memory during the running process, but it does not release the memory until it ends. Strictly speaking, there is no memory leakage because the program releases all requested memory. However, if a server program needs to run for several days, weeks, or even months, failing to release the memory in time may eventually exhaust all the system memory. Therefore, we call this type of Memory leakage an implicit memory leak. (Baidu encyclopedia)
Detection tools
There are many methods to detect memory leaks. The following lists common memory leak detection tools.
Visual leak detecter
Application Environment: Windows + VC
Programming Language: C/C ++
Usage: you only need to include the header file VLD. h and add the provided lib
Result output: It is output to the debugging window of VC.
Design Concept: register the _ crtsetallochook hook function and use the CRT debug heap provided by VC.
Advantages and disadvantages: You can obtain the call stack of the memory leak point and complete data of the memory leak.
Get: http://www.codeproject.com/Articles/9815/Visual-Leak-Detector-Enhanced-Memory-Leak-Detectio
Bounds checker
Application Environment: Windows + vc6.0
Programming Language: C/C ++
Usage: Installation and usage. The shortcut menu is automatically created in VC.
Result output: It is output to the debugging window of VC.
Design Concept: Unknown
Advantages and disadvantages: it can detect memory leakage, resource leakage, Pointer Error operations, memory read and write overflow, and uninitialized memory
How to obtain: http://3ddown.com/soft/31594.htm. when installing licence, change the date to 2008, and then install licence. A license is displayed as 8.3 after being installed, but can be used.
Mtrace
Application Environment: Linux glibc
Programming Language: c
Usage: contains the header file mcheck. H, defines the environment variable malloc_trace as the output file name, and CALLS mtrace () at the beginning of the program.
Result output: The file specified by the user.
Design Concept: Add hook functions for the malloc, realloc, and free functions to record every execution of malloc-free
Advantages and disadvantages: only the memory leakage caused by malloc/realloc/free can be checked.
How to obtain: Glibc comes with direct use
Memwatch
Application Environment: Linux
Programming Language: c
Usage: Add memwatch. H, and add-dmemwatch-dmw_stdio and memwatch. C during compilation.
Result output: the output file name is memwatch. log. During program execution, error messages are displayed on stdout.
Design Concept: redefines malloc, realloc, calloc, strdup, and free as mwmalloc (SZ, _ file __, _ line _), and maintains an operational linked list internally.
Advantages and disadvantages: Double-free, erroneous free, unfreed memory, overflow, and underflow can be detected.
How to Get: http://memwatch.sourceforge.net/
Valgrind
Application Environment: Linux
Programming Language: C/C ++
Usage: Add memwatch. H, and add-dmemwatch-dmw_stdio and memwatch. C during compilation.
Result output: the output file name is memwatch. log. During program execution, error messages are displayed on stdout.
Design Concept: maintain a valid address space table and an Invalid Address Space table (process address space) based on the memory operation of the software)
Advantages and disadvantages: ability to detect:
- Use uninitialized memory (use of uninitialised memory)
- Use the released memory (reading/writing memory after it has been free 'd)
- Use the memory space that exceeds the malloc allocation (reading/writing off the end of malloc 'd blocks)
- Unauthorized stack access (reading/writing inappropriate areas on the stack)
- Whether the requested space is released (memory leaks-where pointers to malloc 'd blocks are lost forever)
How to Get: http://valgrind.org/
Debug_new
Application Environment: Linux/Windows
Programming Language: C ++
Usage: contains the header file debug_new.h. The link is debug_new.cpp.
Result output: Console
Design Concept: capture memory application/release requests by reloading the new and delete operators, and maintain a hash linked list of Global static variables within the program. In the new operator, we not only allocate the memory required by the user, but also add a header for each memory allocation, storing the location information for the allocation and the linked list pointer, new returns the value of the allocated memory plus the header offset, which has previously been hash calculated and added to the hash linked list. During the delete operation, hash calculation is performed based on the pointer address to be released, and then the linked list at the hash value of the array is traversed for search. If it is found, the node is removed. If it is not found, abort is performed. After the program ends, check whether there are unreleased memory blocks in the array to determine whether memory leakage exists.
Advantages and disadvantages: cross-platform, only used for C ++ programs,
Get: http://www.ibm.com/developerworks/cn/linux/l-mleak2/index.html
Summary
The above analysis tools use the following methods:
1. register the memory allocation/release hook function ). In Linux, you can use five hook functions, such as malloc_hook and free_hook. In Windows, you can register the _ crtsetallochook hook function. In this way, you can capture and process the request when allocating memory. Visual leak detecter and mtrace use this method.
2. Use macro definition replacement. Replace malloc and free in your code with macro-defined custom functions such as mwmalloc (SZ, _ file __, _ line _) to track memory requests, memwatch uses this method.
3. Operator overloading. This method is only used in C ++ to track memory requests by reloading the new and delete operators. The overloaded operators are similar to hook functions. Debug_new adopts this method.
The output methods of these tools are as follows:
1. The windows VC environment is generally output to the debugging window. Therefore, the VC itself provides an ideal place for output, and the GUI application is invisible when output to the standard output. Visual leak detecter adopts this method.
2. output to standard output or standard error output: the console application can output to the screen, such as memwatch, valgrind, and debug_new.
3. output to log files: output the results to user-specified or default log files, such as mtrace and memwatch.
In addition, these tools have two memory detection methods:
1. Maintain a memory operation linked list. When there is a memory Application Operation, add it to this linked list. When there is a release operation, remove it from the application operation from the linked list. If there is still content in the linked list after the program ends, it indicates that the memory has been leaked. If the memory operation to be released does not find the corresponding operation in the linked list, it indicates that it has been released multiple times. This method has built-in debugging tools of VC, visual leak detecter, mtrace, memwatch, and debug_new.
2. Address Space of the simulated process. Maintain an address space ing in the user mode following the operating system's processing of Process Memory operations. This method requires a deep understanding of process address space processing. Because the address space distribution of Windows processes is not open source, it is difficult to simulate it. Therefore, it only supports Linux. Valgrind is used.
Next work
I have learned about so many memory detection tools and have some insights by comparing their implementation methods and advantages and disadvantages. However, it is better to write a memory detection task by yourself if you have a short mouth and a soft hand. We strive to learn from the advantages of the above tools and make some innovations. The most important thing is that it must be cross-platform! For more information, see the next article...
Comparison of VLD, bounds checker, memwatch, mtrace, valgrind, and debug_new Memory leakage detection tools