What is a Memory Leak?
Memory leak (Memory Leak) refers to the heap memory that has been dynamically allocated in the program due to some reason the program is not released or cannot be released, causing waste of system memory, causing the program to slow down or even crash the system.
- Memory leak (Memory Leak) refers to the heap memory that has been dynamically allocated in the program due to some reason the program is not released or cannot be released, causing waste of system memory, causing the program to slow down or even crash the system.
- Memory leak defects are hidden and cumulative, and harder to detect than other memory illegal access errors. The reason for the memory leak is that the memory block is not released, which is a missing defect rather than a fault type defect. In addition, memory leaks usually do not directly cause observable error symptoms, but gradually accumulate, reducing the overall system performance, and in extreme cases may cause the system to crash.
- With the increasing demand of computer applications, the design and development of application programs are correspondingly more and more complicated, and the variables handled by developers in the process of program implementation have also increased significantly. How to effectively allocate and release memory to prevent the problem of memory leaks from changing Got more and more prominent. For example, server application software needs to run for a long time and continuously process requests from clients. If there is no effective memory management, there will be a certain memory leak every time the request information is processed. This not only affects the performance of the server, but may also cause the entire system to crash. Therefore, memory management has become a major aspect for software design developers to consider in their design. [1]
- In the C language, from the perspective of the life cycle of variables, variables are divided into static storage variables and dynamic storage variables. The static storage variable refers to a variable that is allocated a fixed storage space during program running, and the dynamic storage variable refers to a variable that dynamically allocates storage space according to actual needs during the program running. The memory space for users in memory is divided into three parts:
- Program memory
- Static memory
- Dynamic memory
- The data used in the program are stored in the static storage area and the dynamic storage area. The static storage area data is allocated at the beginning of the program. The memory unit they occupy is fixed during the entire program execution process and is released at the end of the program. Therefore, the static storage area data is generally a global variable. The dynamic storage area data is a storage unit that is dynamically allocated and released as needed during the execution of the program. The dynamic storage area data has three types of function parameter variables, local variables, and on-site protection and return addresses when the function is called. Because dynamic storage variables can dynamically allocate and release storage space according to the needs of function calls, greatly improving the efficiency of memory usage, making dynamic storage variables widely used in programs.
- When developers use dynamic storage variables in the process of program development, it is inevitable to face the problem of memory management. The dynamically allocated storage space in the program needs to be released after the program execution is completed. The main problem of using dynamic storage variables is to cause memory leaks without releasing dynamically allocated storage space. In general, developers use the basic memory management functions provided by the system, such as malloc, recalloc, calloc, free, etc., to complete the allocation and release of dynamic storage variable storage space. However, when dynamic storage variables are used more frequently and function calls are frequently used in development programs, memory management errors often occur, such as:
- Allocate a block of memory and use its uninitialized contents;
- Free a block of memory, but continue to reference its contents;
- The allocated memory space in the sub-function does not release the allocated memory when the main function breaks abnormally, or when the main function ends the use of the information returned by the sub-function.
- The temporary memory allocated during the implementation of the program did not release the temporary memory at the end of the program. Memory errors are generally not reproducible, and it is not easy for developers to find them during program debugging and testing. Even if they spend a lot of effort and time, they cannot be completely eliminated.
- Classification of production methods
- To classify in a generated way, memory leaks can be divided into four categories:
- Frequent memory leaks: Code that leaks memory will be executed multiple times, each time it will cause a memory leak.
- Infrequent memory leaks: Memory leaking code can only occur under certain specific circumstances or operations. Frequent and occasional are relative. For certain circumstances, occasional may become frequent. So the test environment and test methods are critical to detecting memory leaks.
- One-time memory leak: Code that has a memory leak will only be executed once, or due to a flaw in the algorithm, there will always be one and only one memory leak.
- Implicit memory leak: The program continuously allocates memory during the running process, but does not release the memory until the end. Strictly speaking, there is no memory leak here, because the final program releases all the requested memory. But for a server program, it needs to run for several days, weeks or even months. Failure to release the memory in time may also lead to the exhaustion of all memory of the system. Therefore, we call this type of memory leak an implicit memory leak. From the user's point of view, a memory leak itself does not cause any harm. As a general user, the existence of a memory leak is not felt at all. What is really harmful is the accumulation of memory leaks, which will eventually exhaust all the memory of the system. From this perspective, a one-time memory leak is not harmful because it does not accumulate, while an implicit memory leak is very harmful because it is more difficult to detect than a frequent and occasional memory leak. .
- Regardless of whether it is a C or C ++ program, there are three main ways to allocate variables at runtime: heap allocation, stack allocation, global and static memory allocation. The memory leak mainly occurs in the heap memory allocation method, that is, "after the memory is configured, all pointers to the memory are lost." Without a garbage collection mechanism such as language, such memory chips cannot be returned to the system. Because the memory leak is a problem in the running of the program and cannot be identified through compilation, it can only be identified and diagnosed during the running of the program. The following will introduce several commonly used memory detection methods, each of which uses an existing memory detection tool as an analysis example, and compares the various methods.
- Static analysis technique
- Static analysis technology is to directly analyze the source code or machine code of the program to obtain some useful information without running the program itself. At present, there are many static analysis tools. The compiler belongs to this category. It reads the source program code, performs lexical and grammatical analysis on the source program, checks the data type, and performs some optimization analysis. Quality and operational efficiency. This kind of static analysis tool only reads the program code for related analysis, and does not perform other additional operations, such as modifying the source program code.
- LCLink is a program comprehension and error detection tool that performs static analysis on source code and comments in a specific format added to the source code. The inspection object is the source program. The memory errors that can be checked out include memory allocation release faults, empty Program errors such as misuse of pointers, use of undefined or freed memory.
- LCLink focuses on analyzing two types of memory release errors:
- An attempt was made to free a memory block with two or more valid pointers pointing to it.
- An attempt was made to free a memory block without any valid pointer to it.
- The solution to this type of memory error is to specify that the pointer returned when a block of memory is allocated must free that memory. Use a comment to indicate that a pointer is the only pointer to a memory block. Use a comment to indicate that the called function may release the memory block pointed to by the function parameter or create a new pointer to the memory block.
- Source code instrumentation
- In order to obtain the dynamic execution information of the tested program, it needs to be tracked, and the instrumentation method is generally used. The so-called instrumentation is based on maintaining the logical integrity of the program under test, inserting a test program, also called a probe function, in a specific part of the program under test, and throwing out the program's operating characteristic data through the execution of the probe. Based on the analysis of these characteristic data, the control flow and data flow information of the program can be obtained, and then dynamic information such as logical coverage can be obtained, so that the detection work of the program can be performed dynamically and synchronously during the execution of the program under test. The instrumentation method is further divided into source code level program instrumentation and object code level program instrumentation.
- Some tools
- 1.ccmalloc-Simple use of memory leaks and malloc debug libraries for C and C ++ programs under Linux and Solaris.
- 2.Dmalloc-Debug Malloc Library.
- 3.Electric Fence-malloc () debugging library written by Bruce Perens in the Linux distribution.
- 4.Leaky-a program for detecting memory leaks under Linux.
- 5.LeakTracer-Track and analyze memory leaks in C ++ programs under Linux, Solaris and HP-UX.
- 6.MEMWATCH-written by Johan Lindh, is an open source C language memory error detection tool, mainly through the precessor of gcc.
- 7.ValgrindDebugging and profiling Linux programs, aiming at programs written in C and C ++.
- 8.KCachegrindA visualization tool for the profiling data generated by Cachegrind and Calltree.
- 9. IBM Rational PurifyPlus-helps developers pinpoint performance and reliability errors in C / C ++, managed .NET, Java, and VB6 code. PurifyPlus combines features such as memory error and leak detection, application performance description, and code coverage analysis into a single, complete toolkit.
- 10.ParasoftInsure ++-A runtime error automatic detection tool for C / C ++ applications. It can automatically monitor C / C ++ programs and find errors such as memory corruption, memory leaks, pointer errors, and I / O. And by using a series of unique technologies (SCI technology and mutation testing, etc.), we thoroughly check and test our code, pinpoint the exact location of the error and give detailed diagnostic information. Can run as a plug-in for Microsoft Visual C ++.
- 11. Compuware DevPartner for Visual C ++ BoundsChecker Suite-Runtime error detection and debugging tool software for C ++ developers Runs as a plug-in for Microsoft Visual Studio and C ++ 6.0.
- 12.Electric Software GlowCode-Including memory leak checking, code profiler, function call tracking and other functions. Provides complete error diagnostics and runtime performance analysis toolkit for C ++ and .Net developers.
- 13. Compuware DevPartner Java Edition-Contains several major functional modules such as Java memory detection, code coverage testing, code performance testing, thread deadlock, and distributed applications.
- 14.Quest JProbe-Analyze memory leaks in Java.
- 15. ej-technologies JProfiler-a full-featured Java profiling tool dedicated to analyzing J2SE and J2EE applications. It combines profiling of the CPU, threads, and memory into one powerful application.
- 16.BEAJRockit-Used to diagnose Java memory leaks and point out the root cause. It is specifically optimized for Intel platforms and optimized to get the highest performance on Intel hardware.