"Java Memory leak 5 Summary"

Source: Internet
Author: User
Tags garbage collection

memory leak definition: an object or variable that is no longer used by the program also occupies storage space in memory.

Because the Java JVM introduced the garbage collection mechanism, the garbage collector automatically reclaims objects that are no longer in use, knowing that the JVM recycling mechanism knows that the JVM uses reference counting and a accessibility analysis algorithm to determine whether an object is no longer in use, essentially determining whether an object is still referenced. In this case, because of the different implementation of the code there will be many kinds of memory leak problems (let the JVM mistakenly think that this object is still in the reference, can not be recycled, resulting in memory leaks).

1, Static collection class , such as HashMap, LinkedList and so on. If these containers are static, their lifecycle is consistent with the program, and the objects in the container will not be released until the program ends, causing a memory leak. In simple terms, long lifecycle objects hold references to short lifecycle objects, although short-lived objects are no longer used, but are not recycled because long lifecycle objects hold their references.

2, a variety of connections , such as database connections, network connectivity and IO connectivity. In the process of operating a database, you first need to establish a connection to the database, and when you no longer use it, you need to call the Close method to release the connection to the database. The garbage collector reclaims the corresponding object only if the connection is closed. Otherwise, if you do not explicitly close the connection, statement, or resultset during the access to the database, a large number of objects cannot be reclaimed, causing a memory leak.

3, the variable is not reasonable scope. in general, the definition of a variable is more scoped than it is used for, and is likely to cause a memory leak. On the other hand, if the object is not set to NULL in a timely manner, it is likely to cause a memory leak.

public class Usingrandom {
	
	private String msg;
	public void Receivemsg () {
		readfromnet ();//Accept data from the network to save to MSG
		savedb ()//Save MSG to database
	}

As the above pseudocode, by Readfromnet method to save the received message in the variable msg, and then call the Savedb method to save the contents of MSG in the database, then MSG is useless, because the life cycle of MSG is the same as the life cycle of the object, at this time msg can not be recycled , resulting in a memory leak.

In fact, this MSG variable can be placed inside the Receivemsg method, and when the method is used, the MSG lifecycle is over and can be recycled. There is also a way to set MSG to NULL after using MSG, so that the garbage collector can also reclaim the MSG memory space.

4, the internal class holds the external class, if the method of an external class instance object returns an instance object of an inner class, the inner class object is long referenced, even if the Outer class instance object is no longer used, but because the inner class holds an instance object of the external class, This external class object will not be garbage collected, which can also cause memory leaks.

5, changing the hash value, when an object is stored in the HashSet collection, you cannot modify the fields in this object that 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 Even if the Contains method retrieves an object in the HashSet collection using the current reference of the object as an argument, the result of the object cannot be found, which also results in the inability to delete the current object individually from the HashSet collection, causing a memory leak


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.