1. definition of memory leaks
In general, the memory leaks that we often say refer to the leaks in heap memory. Heap memory means that the program is allocated from the heap, arbitrarily sized (the size of the memory block can be determined during the program's run time), and the freed memory must be displayed after use. Applications typically use malloc,realloc,new , and other functions to allocate a chunk of memory from the heap, and after use, the program must be responsible for the corresponding call Free or Delete Release the memory block, otherwise, the memory will not be reused, we will say this memory leak.
2. Consequences of memory leaks
The program runs out of control and consumes more and more memory over time (such as background tasks on servers, especially background tasks in embedded systems, which may be ignored for many years after they are run);
New memory is allocated frequently, such as when displaying computer games or animated video images;
The program can request memory that is not freed (such as shared memory), even when the program terminates;
Leaks occur within the operating system;
Leakage occurs in the critical drive of the system;
Memory is very limited, such as in embedded systems or portable devices;
When running on a termination, the memory is not automatically released on top of the operating system (such as AmigaOS), and once lost can only be restored by a reboot.
3. How to discover memory leaks
Some simple memory leak issues can be determined from the check-in phase of the code. There are some more serious leaks, that is, in a short period of time to cause the program or system crashes, or the system reports that there is not enough memory, it is easier to find. The most difficult thing is that the leak is slow, and it takes a few days, weeks, or months to see the obvious anomalies. So how to detect a potential memory leak in a relatively short period of time? In fact, different systems have memory monitoring tools, and we can collect the stack memory information from the monitoring tool for a period of time and observe the growth trend to determine if there is a memory leak. In the Linux platform, you can use the PS command to monitor memory usage, such as the following command ( to observe the VSZ of the specified process Value )
Ps-aux
Static analysis, including manual detection and static tools, is the least expensive method.
1/when usingC + +when developing, adopting good and consistent programming specifications is the first and most important measure to prevent memory problems. Testing is a supplement to the coding standard. Both of them are beneficial, but the combination of the use of the effect is particularly good. of professionalCorC + +professionals can even browse unfamiliar source code and detect memory problems at a very low cost. With a small amount of practice and proper text search, you can quickly verify the balanced*alloc ()and theFree ()orNewand theDeletethe source body. Manually viewing this type of content will typically appear like a checklist1The same problem that can be located in the functionleaktestthe heap variables inlogmsgnot released.
2/ code static scanning and analysis tools more, such as splint, Pc-lint, BEAM and so on. Because BEAM Support More platforms, this take BEAM As an example, do a brief introduction, others have a similar process.
BEAM can detect four types of problems : non-initialized variables, discarded null pointers, memory leaks, redundant computations. And there are more platforms supported.
BEAM supports the following platforms:
Linux x86 (glibc 2.2.4)
Linux s390/s390x (glibc 2.3.3 or higher)
Linux (PowerPC, USS) (glibc 2.3.2 or higher)
AIX (4.3.2+)
Window2000 or more
or use the inline program to automatically detect
You can overload memory allocations and release functions new and Delete, and then write programs that periodically count memory allocations and releases to find possible memory leaks. or call system functions to periodically monitor the size of the program heap, it is critical to determine that the heap growth is a leak rather than a reasonable memory usage. This kind of method is more complicated, so we don't give a detailed example.
2. Dynamic Operation detection
Real-time detection tools are mainly Valgrind, Rational purify and so on.
1, Valgrind
Valgrind is a tool to help programmers find bugs in programs and improve program performance. When the program runs through valgrind ,valgrind collects a variety of useful information that can be used to find potential bugs in the program and performance bottlenecks.
ValgrindNow there are a number of tools, the most important of which isMemcheck,Cachegrind,Massifand theCallgrind. Valgrindis inLinuxTools for debugging memory problems when developing applications under the system. It is particularly adept at discovering memory management issues, which can check for memory leaks when the program is running. One of theMemechecktools can be used to findC,C + +errors in memory management in the program. You can check for errors on the following types of memory operations:
Read and write memory that has been freed
Read and write memory blocks out of bounds (formerly or from behind)
Using variables that have not yet been initialized
Passing meaningless arguments to a system call
Memory leaks
2,Rational purify
rational purify mainly for memory errors that are difficult to discover during software development, Run-time error. Reduce commissioning time by automatically discovering errors during software development, locating errors accurately, and providing complete error information. It is also the only similar tool on the market that supports multiple platforms and integrates with many mainstream development tools. PURIFY  You can check every module of your application, You can even detect errors in complex multithreaded or process applications. In addition, you can check , c/c++ JAVA  or .NET 
In a Linux system, using Purify requires recompiling the program. It is common practice to modify The compiler variables in Makefile. The following is the Makefile used to compile the program in this article :
Cc=purify GCC
First run the purifyplus_setup.sh in the Purify installation directory to set the environment variables, and then run the Make Recompile the program.
./purifyplus_setup.sh
An example of compiling a code file is given below, and the source code file is named test3.cpp. with Purify and the g++ the Compile command is as follows, ' - G ' is compile-time plus debug information.
Purify g++-G test3.cpp –o test
Run the compiled executable test, you can get figure 1, you can locate the specific location of the memory leak.
./test
4. How to avoid memory leaks
In fact, the cause of memory leaks can be summarized as: Call the malloc/new and other memory applications, but the lack of corresponding free/delete, in short,malloc/ New is more than the number of free/delete. We need to pay attention to this in programming, to ensure that each malloc has a corresponding free, each new has a corresponding deleted!!! Usually want to develop such a good habit.
To avoid a memory leak, you can summarize the following points:
1, programmers should develop good habits, to ensure that malloc/new and free/delete match;
2, over and over again look at the code, hoping to see the memory leak of the bug
3, check whether malloc/new and free/delete match, some tools that is the principle. To do this, you can use a macro or a hook to add a layer between the user program and the runtime to record memory allocations.
4., summed up a number of laws: to use, the free memory pointer to empty, in as few functions as possible to apply and free memory, and so on
5, the use of a large number of memory village leak detection tools, combined with a variety of automated testing software, to take crazy and abnormal test methods, to try to find all possible memory leak bugs.
6, code size more than tens of thousands of lines, memory from one module is passed to another or more, who knows which module to pass to, finally released there, anyway I guarantee that the pointer to the data is good on the line. Because of the size of the code, exhaustive testing is not possible, only to look at the code over and over again, praying that their code does not have a memory leak problem.
Memory fragmentation
1. What is memory fragmentation
Memory Fragmentation --- describes all the unused free memory in a system, and the resources are still not being used because the allocator responsible for allocating memory is making the memory unusable. This problem usually occurs because free memory appears in a different location in small, discontinuous ways. Because the allocation method determines whether memory fragmentation is a problem, the memory allocator plays an important role in guaranteeing the availability of idle resources.
2. Causes of memory fragmentation
Cause occurs in a different location with free memory in a small, discontinuous way(memory allocations are small, and the allocated small memory lifetimes are longer, and the memory fragmentation occurs after repeated requests). There are three basic ways that memory-allocation programs waste memory: extra overhead, internal fragmentation, and external fragmentation (Figure1). The memory-allocation program needs to store some data that describes its allocation state. These stored information includes the location, size, and ownership of any free memory block, as well as other internal state details. In general, the best place for a run-time-allocation program to hold this extra information is the memory it manages. The memory allocation program needs to follow some basic memory allocation rules. For example, all memory allocations must start at the point where they can be4,8or -an address that is divisible (depending on processor architecture). The memory allocation program allocates only the predetermined size of memory blocks to the customer, and there may be other reasons. When a customer requests a +byte of memory block, it may get -bytes, -bytes, or even more bytes. The extra space generated by rounding the desired size is called internal fragmentation.
External fragmentation is generated when an unused difference occurs between allocated memory blocks, resulting in external fragmentation. For example, an application allocates three contiguous blocks of memory and then makes a block of memory in the middle idle. The memory-allocation program can reuse the intermediate memory blocks for future allocations, but it is unlikely that the allocated blocks will be as large as all free memory. If the memory-allocation program does not change its implementation and rounding policy during run time, the additional overhead and internal fragmentation remain constant throughout the system's lifetime. While extra overhead and internal fragmentation can waste memory, it is undesirable, but external fragmentation is the real enemy of embedded system developers, and it is the distribution problem that causes the system to fail.
3. The drawbacks and advantages of memory fragmentation
Disadvantages:
A large amount of memory fragmentation can make the system slow, because the use of virtual memory makes the data exchange between memory and hard disk known as System
Slow roots , eventually causing the depletion of memory !
Advantages:
Reduce memory fragmentation, increase allocation speed, facilitate memory management, and prevent memory leaks
4, how to avoid the generation of memory fragmentation
1, less function with dynamic memory allocation ( use stack space as much as possible )
2, allocating memory and releasing memory as much as possible in the same function
3, try to apply a large amount of memory 2 of the exponential power size of the memory space, instead of repeatedly applying for small memory ( less memory segmentation )
4, using a memory pool to reduce memory fragmentation caused by using heap memory
5, to apply for space as little as possible.
6 use as little memory space on the heap as possible ~
7 do a memory pool, that is, one time to apply for a large enough space, and then manage, for a large number of frequent new/delete operations.
The memory management system will be able to merge adjacent free memory blocks in a hurry, resulting in greater free memory. This does not cause memory fragmentation to occur. Even if the adjacent space is not idle, the resulting fragments are relatively small, but for the game ( longer running ) or the phone ( memory is small )
C + + memory leaks and memory fragmentation generation and avoidance strategies