Relationship between memory leaks and the OOM

Memory leak reaches a certain level will lead to OOM .
Memory leaks are a code you write contains bug, not referring to the kind of program execution error will lead to results, but unreachable objects remain in the heap, that strong tags contain no references to objects not freed, resulting in the useless objects can not be garbage collected.
Assume that memory is large enough, and the memory leak situation is not serious, as long as there is enough space to allocate new objects, so even a memory leak will not lead OOM.

Several common causes of memory leaks

Static collections caused by a memory leak

With the class static variable is assigned to the method of loading zone, as long as the class has not been uninstalled, static variables will not be recovered, and a collection of static objects (refer Collection object, List, Set, Map), added to it the collection each object in the collection will hold its reference, remove as long as we are not shown in the code.

After HashSet object properties to be modified, then calling remove ineffective.

HashSet (the inside of the package HashMap). First of all we need to know Map collection is to store an object by pairs. After several attempts and view the source code and found that when an object is to add storage HashSet object when hashCode () Returns the value of and references as key-value pairs stored inside.
In other words, if we want to modify its reference object properties can still be removed (HashSet.remove (Object obj)) from HashSet, we need to ensure that no matter how the properties of an object changes its hashCode () method the returned value are consistent.

Monitor

We often need to set up a listener to monitor a control state, we have no need to pay attention to the corresponding listener also freed (listener holds a reference to the control) at the time of release control.

Various connections

Such as a database connection (dataSourse.getConnection ()), network connection (Socket) and io connection, unless explicitly call its close () method to which the connection is closed, otherwise it is not automatically recovered by GC. For Resultset and Statement objects can not be recycled explicitly, but must be explicitly Connection recycled because Connection can not be automatically recovered at any time, but once recovered Connection, Statement and Resultset objects will immediately be NULL. But if you use connection pooling, the situation is different, in addition to explicitly close the connection, you must explicitly close Resultset Statement objects (close one, the other one will be closed), otherwise it will cause a lot of Statement objects can not be released, causing a memory leak. Usually connected to try this case the inside, which finally releases connections.

Call an external module

The most direct example is the callback. For example, we create a class object A and a class B object code, a B class object method call in object class A and class B need the callback object, A, class B objects holding each other mutual references , even if we release the final code in reference to the two objects, GC can not recover these two objects, resulting in a memory leak.

Singleton

Previous article also mentioned a little before, the first clear singleton object is static, and that they hold their own strong reference, that the life cycle can be equivalent to the life cycle of the application process will not be running GC recycling, then it is clear if the singleton object to hold strong references to other objects, that object is referenced also can not be recycled, especially when the singleton object contains a reference to a collection of objects, pay attention to the referenced release .

Common memory overflow exception

Java heap overflow

java.lang.OutOfMemoryError: Java heap space

Result in a heap overflow space can not heap after one still can not allocate enough space for the new object to allocate memory space and GC.

Stack overflow

java.lang.StackOverflowError

This is the maximum stack depth due to the depth of the thread requested is greater than allowed by the virtual machine can not be pushed into a new stack frame, then you can check whether there is an infinite loop in a recursive call code.

Methods overflow

java.lang.OutOfMemoryError: PermGen space

Here it can also be divided into runtime constant pool overflow storage area and method Class excessive lead to overflow. The size of the area in the past, then the method is to set a good, after Java8 the Class information into the element space, and space is the yuan can apply for extension of dynamic, detailed descriptions small search for a permanent partner can own behalf to the relevant blog yuan space.

Unable to create new process

java.lang.OutOfMemoryError:unable to create natvie thread

Is a lack of stack space can not create a new process.

Note: Transfer Jane books

Guess you like

Origin www.cnblogs.com/sunpengblog/p/11908396.html