There is a memory leak in Java, please describe it briefly. __java

Source: Internet
Author: User
Tags object object

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.

Added: 3. When an object is stored in the HashSet collection, you cannot modify which fields in this object participate in the calculation of the hash value, otherwise the modified hash value of the object is different from the hash value originally stored in the HashSet collection, in which case the contains () method uses the current reference of the object Retrieves the object in the HashSet collection as a parameter, and returns the result of the object that cannot be found, which also results in a memory leak that cannot be deleted from the HashSet collection individually.

A simple example of a memory leak is given below. In this example, we loop through the object object and put the application in a vector, and if we just release the reference itself, then the vector still references the object, so the object is not recyclable for the GC. Therefore, if the object is added to the vector, it must also be removed from the vector, and the simplest way is to set the vector object to null.

public class Memoryleak {public
	static void Main (string[] args) {
		vector v = new vector (a);
		for (int i = 0; i < i++) {
			object o = new Object ();
			V.add (o);
			O=null
		}}
	}

At this point, all object objects are not freed because the variable v refers to these objects.

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.

Turn from: 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.