I recently heard about the memory leakage seminar of memory leak, and I felt a bit rewarding. So I kept a record and shared it with my friends.
1. What is memory leak.Memory Leak indicates that some declared object instances occupy memory for a long time and cannot be recycled due to errors or incomplete code. Memory Leak may cause system performance degradation or system errors.
2 memory storage modeThe storage status of C ++ or Java code in the memory is as follows.
In short, local variables are generally stored in the stack to increase the running speed. The new variables store the reference information or pointer in the stack, and store the object itself in the heap area.
Here, I would like to thank Jun Xiao for providing the following link after reading the blog, so that you can better understand the concepts of stack and stack.
Http://www.builder.com.cn/2007/1010/544483.shtml
3. Cause of Memory Leak encoding and Avoidance
There are three reasons for Memory Leak.
(1) No referenced memory (C ++ only)
Sample 1 (){
Dkstring * s = new dkstring ();
... ...
... ...
Delete S;
}
Sample 2 (){
Char * buffer = (char *) malloc (64 * sizeof (char );
... ...
... ...
Free (buffer );
}
There are two ways to generate/release variables in C ++: New/delete, malloc ()/Free (). No matter how it is generated, the release action must be triggered.
Otherwise, when the program leaves the variable scope, the reference in the stack will be automatically recycled, but the object instance in the heap itself will become the no referenced memory permanently abandoned in the memory.
In addition, you must use Delete to release a new file;
If it is generated using malloc, use free for release. On the contrary, although the code can be compiled, it may cause many potential problems.
(2) No free objects/pointers (C ++ and Java)
Java is more convenient than C ++, where Java can recycle expired memory spam and GC from the slave node. Therefore, Java Programmers never care about delete or free.
However, GC is powerless in this case, let alone C ++.
Sample 3
String [] SA = new string [9999999];
For (INT I = 0; I <9999999; I ++ ){
String S = new string ("adfasdfadsfas... Adfasdfa "); // A 1 MB Size String...
Sa [I] = s;
}
This code depressing GC is that GC will never receive any space before the cycle ends.
Because GC can only collect those expired variables, but before SA expires, outofmemory may have occurred.
(3) No limited storage (C ++ and Java)
Sample 4... ...
While (true ){
Vector. Add (OBJ );
}
... ...
Like vector, hashtable, hashmap, MAP, arraylist and string stringbuffer... ... There is no upper limit for such a tool class. If the developer does not add any control, it is easy to overflow the memory.
4 how to discover memory leak through testing
(1) Long Run
In many cases, the tiny memory leak will not cause too much impact on our system. Only when the leakage reaches a certain level will the problem break out. Therefore, theoretically, we need to repeat the code multiple times to expose the memory leak problem. In our company, this test is called long run. The so-called long run does not necessarily mean that the test case must be run for a long time, but a sufficient number of times.
(2) Special Case
This is a tester practice. They believe that developers will consider and handle the memory leak issue in most normal program logic, but the exception conditions may not be necessary, the case of memory leak can be found through appropriate critical tests and special case tests.
I personally think that, to discover and avoid Memory leak, the most important thing is that the developer should control the quality in the upstream when designing and coding the program. Otherwise, when tester discovers and locates the memory leak problem, the cost is quite high.
5. Memory Leak analysis tool
To do well, you must first sharpen your tools. Here we will search for and constantly add some memory leak analysis tools to meet the requirements of tool people.
(1) purify General for C code
(2) Heap Analyzer
For Java code
(3) Java dump
This involves another topic, and I hope it can be supplemented later.
Http://blog.csdn.net/iloveagile/article/details/3850668
Sweating obfuscated the memory overflow and Memory leakage. The latter two situations do not refer to memory leakage, but memory overflow.
In general, memory leakage means that a program has applied for a certain piece of memory. If no memory is recycled, the system determines that the memory is still occupied. Therefore, the memory [equivalent] disappears.
Memory overflow means that the program does not have enough memory space to use when applying for memory.
The connection between the two is: Memory leakage will eventually cause memory overflow if it is accumulated
Memory overflow out of memory means that the program does not have enough memory space to use when applying for memory. For example, if an integer is applied, however, if a long value is saved to it, the memory overflow occurs.
Memory leakage Memory Leak refers to the failure of the program to release the applied memory space after applying for memory. The memory leakage hazard can be ignored once, but the memory leakage accumulation result is very serious, no matter how much memory, sooner or later.
Memory leak will eventually cause out of memory!
Memory overflow means that the amount of memory you need to allocate exceeds what the system can offer. If the system cannot meet your needs, it will cause an overflow.
Memory leakage means that you apply to the system to allocate memory for use (new), but do not return it after use (delete ), as a result, the memory you applied for cannot be accessed by yourself (maybe you have lost its address), and the system cannot allocate it to the required program again. A plate can only contain four fruits in a variety of ways. If you have five fruits, you cannot eat them if you drop them. This is overflow!
For example, stack overflow occurs when the stack is full. Stack Overflow also occurs when the stack is empty. That is, the allocated memory is insufficient to put down the data sequence, which is called memory overflow.
Memory leakage can be classified as follows:
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 only one piece of Memory leakage.
For example, if the class constructor allocates memory but does not release the memory in the destructor, the 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.
From the perspective of user programs, memory leakage does not cause any harm. As a general user, the memory leakage does not exist.
The real danger is the accumulation of Memory leakage, which will eventually consume all the memory of the system.
From this perspective, one-time memory leakage is not harmful because it will not accumulate, while implicit memory leakage is very harmful because it is more difficult to detect than frequent and occasional memory leaks.
Http://blog.csdn.net/buutterfly/article/details/6617375
Memory Leak (Memory leakage)