Summary of Memory leak (memory leaks)

Source: Internet
Author: User
Tags variable scope

Recently listened to some about memory leak (memory leaks) of the seminar, feel some harvest, so leave a record, and share to friends.

1 What is memory leak.
Memory leak means that some declared object instances have long occupied memory space due to faulty or incomplete code, and cannot be recycled. Memory leak can cause system performance to degrade or cause system errors.

2 Memory storage Mode
The storage status of C + + or Java code that we usually write in memory is shown below.

Simply put, general local variables are stored in stack to increase the speed at which the run is asked. The new variable then stores the reference information or pointer in the stack and stores the object itself in the heap area.

Here thank June Xiao Students read the blog after the provision of this link, you can let us better understand what heap ah, stack ah concept.

Http://www.builder.com.cn/2007/1010/544483.shtml

3 encoding causes memory leak, and avoids
The reason of Memory leak is now summed up 3 kinds, hereafter want to have, add again.
(1) No referenced Memory (c + + only)
Sample 1
A () {
dkstring* s= new dkstring ();
... ...
... ...
Delete S;
}

Sample 2
A () {
char* buffer = (char*) malloc (sizeof (char);
... ...
... ...
Free (buffer);
}

There are two ways to generate/release variables in C + +, New/delete, malloc ()/free (). No matter how you generate it in that way, you must finally have a release action. Otherwise, when the program leaves the variable scope, the reference inside the stack is automatically recycled, but the object instance in the heap itself becomes the no referenced Memory that is permanently abandoned in memory.
Also note that if you generate it with new, you must use Delete to release it, and if you generate it with malloc, you must release it with free. Conversely, although the code can be compiled, it creates a number of potential problems.


(2) No free objects/pointers (c + + Java)
Java is more convenient than C + + where Java can recycle expired memory garbage from the garbage, GC. So, Java programmers never have to worry about delete or free. But in this case, the GC is powerless, not to mention C + +.
Sample 3
string[] sa = new string[9999999];
for (int i = 0; i < 9999999; i++) {
string s = new String ("Adfasdfadsfas...adfasdfa"); A 1MB size string ...
Sa[i] = s;
}
This code makes the GC depressed that the GC will never receive any space until the end of the loop. Because the GC can only collect those variables that expire, it is possible that outofmemory already occurred before the SA expires.

(3) No Limited Storage (c + + Java)
Sample 4
... ...
while (true) {
Vector.add (obj);
}
... ...
Like vectors, Hashtable, HashMap, Map, ArrayList and String StringBuffer ... Such a tool class itself does not have the upper limit, if, developer no more control, it is easy to overflow memory.

4 How to find memory leak by testing
(1) Long Run
Very often, the tiny memory leak will not cause too much influence to our system, only when the leakage accumulates to a certain extent, the problem will explode. So, theoretically, we have to let the code repeatedly run to expose the memory leak problem. In our company, this test is called Long Run. Long run does not necessarily mean how long it will take to run the test case, but to run enough times.
(2) Special case
This is a tester experience, and they believe that developer will consider and deal with the memory leak problem in most normal program logic, but the exception condition may not be possible, therefore, by appropriate critical test and special case test, or can find memory Leak's case.

Personally, find and avoid memory leak, the most important thing is to developer from the design, coding in the upstream to good quality. Otherwise, real to tester find and locate memory leak problem, the price is quite big.

5 Analysis of memory leak tools
工欲善其事, its prerequisite. Here will search and continue to add some memory leak analysis tools to meet the tool people requirements.
(1) Purify
General for C Code
(2) Heap Analyzer
Can be for Java Code
(3) Java Dump
This involves another topic and hopes to be supplemented later.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.