Java memory Leak Small example __java

Source: Internet
Author: User
Tags connection pooling
Java memory leaks

Remember the last interview to XX company, an interviewer asked me if there is memory leak in Java, I said there, but then I suddenly Leng, the interviewer immediately to correct me, Java is no memory leaks, I do not dare to debate, I know the situation at that time, this is not a doubt. Back after I checked the data, sorted out, memory leaks in several cases, lest go astray, now to share with you

  a big difference between Java and C + + is that Java has garbage collection, the GC automatically manages the recovery of memory, so developers do not need to invoke methods to free up memory, and C + + does not have the mechanism to automatically reclaim memory, so there is a need to pay attention to memory recovery everywhere, A memory leak may occur if you are not aware of it.

But does this mean that there is no memory leak in Java? The answer is no, and there are some situations that can lead to memory leaks in programs written by Java.

Java Memory recycle mechanism

Regardless of the type of memory allocation, you need to return the address of the allocated memory, which is to return a pointer to the first address of the memory block. Objects in Java are created using new or reflective methods, which are created in the heap (Heap), and all objects are recycled by the Java virtual machine through the garbage collection mechanism. GC in order to properly release objects, will monitor the health of each object, their applications, references, referrals, assignments, and other conditions of monitoring, Java will use a method of mapping to manage memory, real-time monitoring of the object can be achieved, if not reachable, it will be recycled, This can also eliminate the problem of reference loops. In the Java language, there are two criteria for determining whether a single memory space is eligible for garbage collection: one is to give the object a null value NULL, the following has not been invoked, the other is to give the object a new value, so that the memory space is reallocated.

the reason for Java memory leaks

1, static collection classes such as HashMap, vector and other use of the most vulnerable to memory leaks, these static variables of the lifecycle and application consistent, all object objects can not be released, because they will always be vector applications.

2, internal class and external class reference is prone to memory leaks problems

3, the use of the listener, Java is often used to the listener, in the release of the object without the corresponding removal of the listener can also lead to memory leaks.

4, the use of a large number of temporary variables, not in time to set the object to null can also lead to memory leaks

5, the database connection is not closed, including connection pooling method to connect to the database, if not close resultset, etc. can also occur memory leaks.

Example of a memory leak:

Vector v = new vector (10);

for (int i = 1; i<100; i++) {

Object o = new Object ();

V.add (o);

o = null;

}

Don't think O objects are recycled, actually v references these objects, and the referenced object GC is not recyclable, which is likely to lead to memory leaks.

Where the analysis wrong or need to add to the place, I hope to get guidance, thank you

Related Article

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.