[Summary of 5 situations of java memory leak]

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

Since java's JVM introduces a garbage collection mechanism, the garbage collector will automatically recycle objects that are no longer used. Anyone who understands the JVM recycling mechanism knows that the JVM uses reference counting and reachability analysis algorithms to determine whether objects are no longer used. The essence of the object is to determine whether an object is still referenced. In this case, due to the different implementation of the code, there will be many kinds of memory leak problems (make the JVM mistakenly think that the object is still being referenced and cannot be recycled, resulting in memory leaks).

1. Static collection classes , such as HashMap, LinkedList, etc. If these containers are static, their life cycle is consistent with the program, and the objects in the container will not be released before the program ends, resulting in memory leaks. In short, objects with long lifetimes hold references to objects with short lifetimes. Although objects with short lifetimes are no longer used, they cannot be recycled because objects with long lifetimes hold their references.

2. Various connections , such as database connections, network connections, and IO connections. In the process of operating the database, the connection with the database needs to be established first, and when it is no longer used, the close method needs to be called to release the connection with the database. Only after the connection is closed, the garbage collector will recycle the corresponding object. Otherwise, if the Connection, Statement or ResultSet is not explicitly closed during the process of accessing the database, a large number of objects will not be reclaimed, resulting in memory leaks.

3. Unreasonable scope of variables. Generally speaking, the scope of the definition of a variable is larger than the scope of its use, which is likely to cause memory leaks. On the other hand, if the object is not set to null in time, it is likely to cause a memory leak.

public class UsingRandom {
	
	private String msg;
	public void receiveMsg(){
		readFromNet();// Receive data from the network and save it to msg
		saveDB();// save msg to database
	}
}

As shown in the above pseudo code, the received message is saved in the variable msg through the readFromNet method, and then the saveDB method is called to save the content of msg to the database. At this time, msg is useless. Due to the life cycle of msg and the life of the object The cycle is the same, and the msg cannot be recovered at this time, thus causing a memory leak.

In fact, this msg variable can be placed inside the receiveMsg method. When the method is used up, the life cycle of msg will end, and it can be recycled. There is also a way to set msg to null after using msg, so that the garbage collector will also reclaim the memory space of msg.

4. The inner class holds the outer class. If the method of an instance object of an outer class returns an instance object of the inner class, the inner class object is referenced for a long time, even if the outer class instance object is no longer used, but due to the internal The class holds the instance object of the outer class, the outer class object will not be garbage collected, which will also cause memory leaks.

5. Change the hash value. When an object is stored in the HashSet collection, the fields in the object that participate in the calculation of the hash value cannot be modified. Otherwise, the modified hash value of the object will be the same as the one originally stored in the HashSet collection. In this case, even if the contains method uses the current reference of the object as the parameter to retrieve the object in the HashSet collection, it will return the result that the object cannot be found, which will also lead to The current object cannot be deleted from the HashSet collection alone, resulting in a memory leak


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325471789&siteId=291194637