Java memory leaks

Source: Internet
Author: User
Tags object object

First, what is a memory leak:

Memory leaks refer to useless objects (objects that are no longer being used) that persist in memory or the memory of useless objects is not released in a timely manner, resulting in a waste of memory space known as memory leaks.

Second, what is the root cause of the Java memory leak:

Long life-cycle objects that hold references to short life-cycle objects are most likely to have a memory leak, although short life-cycle objects are no longer needed, but because long-life-cycle objects hold their references, they cannot be recycled.

Third, the occurrence of memory leaks in Java scene. There are several major categories:

1. A static collection class causes a memory leak:

Uses such as HashMap, vectors, and so on are most prone to memory leaks, and the lifetime of these static variables is consistent with the application, and all objects referenced by them cannot be freed.

Because they will always be quoted as vectors.

Cases:
Static vector v = new vector (10);
for (int i = 1; i<100; i++)
{
Object o = new Object ();
V.add (o);
o = null;
}//
In this example, the loop applies to the Object object and puts the requested objects into a vector, and if you just release the reference itself (O=null), then the vector still references the object, so the object is not recyclable to the GC. Therefore, if the object has to be removed from the vector after it has been added to the vector, the simplest way is to set the vector object to null.

2. When the object property inside the collection is modified, the Remove () method does not work when it is called.

3. Listener
In Java programming, we all need to deal with listeners, usually one application will use a lot of listeners, we will call a control such as Addxxxlistener () and other methods to increase the listener, but often in the release of objects without remembering to delete these listeners, This increases the chance of memory leaks.

4. Various connections

5. References to internal classes and external modules, etc.

The reference to the inner class is a relatively easy one to forget, and once it is not released it can cause a series of subsequent class objects not to be released. In addition, the programmer should be careful of the external module inadvertently referenced, such as programmer A is responsible for a module, a method called the B module, such as:
public void registermsg (Object b);
This call will be very careful, passing in an object, it is likely that module B will maintain a reference to the object, it is important to note that Module B provides the appropriate action to remove the reference.

6. Single Case mode

Improper use of Singleton mode is a common problem that causes memory leaks, where a singleton object will exist in the JVM's lifetime (in the form of a static variable) after it is initialized, and if the Singleton object holds a reference to an external object, the external object will not be properly reclaimed by the JVM, causing a memory leak.

Java memory leaks

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.