Is there a memory leak in Java, please describe it briefly

Source: Internet
Author: User

Yes. The reason Java causes memory leaks is clear: a long lifecycle object holds a reference to a short lifecycle object and is likely to have a memory leak, although the short lifecycle object is no longer needed, but because the long lifecycle object holds its reference and cannot be reclaimed, this is the scenario where memory leaks occur in Java.

1. Collection class, the collection class has only the method of adding elements, and there is no corresponding deletion mechanism, which causes memory to be occupied. This is actually not clear, this collection class if only a local variable, does not cause memory leaks, after the method stack exit is not referenced by the JVM will be normal recycling. And if this collection class is a global variable, such as a static property in a class, a global map, such as a static reference or final always point to it), then there is no corresponding deletion mechanism, it is likely to lead to the collection of memory only increase, so it is necessary to provide such a removal mechanism or periodic purge policy.

2. Single case mode. Improper use of single case mode is a common problem that causes memory leaks. A single Instance object is initialized to exist throughout the JVM's lifecycle (in the form of static variables), and if a single instance object holds a reference to an external object, the external object will not be properly reclaimed by the JVM, causing memory leaks. Consider the following example:

Class a{

Public A () {

B.getinstance (). SetA (this);

}

....

}

Class B Using a single case model

Class b{

Private a A;

private static B Instance=new B ();

Public B () {}

public static B getinstance () {

return instance;

}

public void SetA (a) {

This.a=a;

}

Getter ...

}

Obviously B uses singleton mode, he holds a reference to a object, and the object of Class A cannot be reclaimed. Imagine what happens if a is a larger object or a collection type.

These also inspire us to look for memory leaks and focus on long lifecycle objects during code review: Global collections, use of single cases, static variables for classes, and so on. In the Java implementation process, also consider its object release, the best way is not to use an object, the object is explicitly assigned to empty. It is best to follow the principle of who created who to release.


Original address: http://blog.csdn.net/yakihappy/article/details/3979942

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.