JVM memory leak causes a scene out of memory (OOM) of

First, the concept

1. Memory leak: After you are finished using the object, is not expected to be recovered in accordance with the GC, has remained in the memory

2. Memory Overflow: a large number of objects have been left in memory, resulting in not enough memory (OOM), affecting normal program run

 

Second, the memory leak scenario

1. The memory data is too big, such as disposable taken out too much data from the database

2. static collections of references to objects, not emptied after use (only the objects set to null, and not removed from the set) the JVM may not be recovered, i.e., memory leaks

3. static method can only use the global static variables, and if static variables and static methods to hold the parameters passed references to objects, can cause a memory leak

Infinite loop, or the presence of excessive cycle 4. The code generating excessive duplicate objects

5. JVM startup parameter memory value is set too small

a heap memory:. JVM default 64M, -Xms stack minimum, maximum -Xmx heap, OutOfMemoryError: java heap space

b stack memory:. -Xss, StackOverflowError, deep stack

c permanent generation of memory:. -XX: PermSize, -XX: MaxPermSize, OutOfMemoryError: PermGen space, excessive loading of class

6. listener: addXXListener, do not remove

7. The various connections not closed: for example a database connection, network connection

Example 8. Single Mode: If the singleton object holds a reference to an external object, the external object will not be recovered, causing the memory leak

9. A class contains static variables, objects of this class can not be recovered

10. ThreadLocal

 

Third, Solutions

1. Modify the JVM startup parameters, directly increase the memory

2. Check the error log

3. Check there is no one-time code to detect all database data

4. Check if there is an infinite loop code

5. Check code cycle and whether a large number of duplicate objects recursively

6. Check List / Map collections, etc., if not cleared

7. Use memory viewer tool

 

Fourth, code optimization

1. active release of useless objects

2. Try to use StringBuilder instead of String

3. minimize the use of static variables, because the static class variable is, not recovering the GC

4. Avoid creating large objects and at the same time creating a plurality of objects, such as an array, because the length of the array is fixed

The object pool technology

 

Five, JVM heap after running out of memory, other threads possibility to continue working?

1. After the current thread OOM, if termination occurs GC, other threads can continue working

2. If the thread OOM, not terminated, other threads will OOM

 

 

 

 

https://www.cnblogs.com/200911/p/3965108.html

 

Guess you like

Origin www.cnblogs.com/june0816/p/11117959.html