Reprinted from: https: //www.cnblogs.com/ThinkVenus/p/6805495.html
Find a lot recently about the OOM, and even Java JVM memory management, and related information and found this stuff too much, as much cluttered feeling, in order to fully understand it, Kongfei article can say clear, Therefore, according to their own understanding of finishing an article, and the rest you need to continue to learn.
1 ) What is the OOM ? OOM, stands for "Out Of Memory", translated into Chinese is "running out of memory", from java.lang.OutOfMemoryError. The official explanation about the look: Thrown when the Java Virtual Machine can not allocate an object because it is out of memory, and no more memory could be made available by the garbage collector means is that when the JVM because there is not enough memory to serve. when the object is allocated space and the garbage collector has no space recyclable, it will throw this error (Note: non-exception, because this problem has been serious enough to be applied to the process).
- Program Counter: the current byte code execution thread line number indicator, thread-private
- JAVA virtual machine stack: Java memory model execution method performed each Java method corresponds to the operation of the stack a stack frame into and out of the stack.
- Native method stacks: similar to "JAVA virtual machine stack", but to provide memory environment for running native methods.
- JAVA stack: place the object memory allocation, memory garbage collection main area, shared by all threads. Can be divided into the new generation, the old generation.
- Method zone: for storing class information have been loaded in the JVM, constants, static variables, the real-time data such as the code compiler. Hotspot in "permanent generations."
- Runtime constant pool: a part information storing method constant region, such as various literal, like reference symbols.
- Direct Memory: not JVM runtime portion of the data area, direct access to memory, such as NIO will use this section.
- java.lang.OutOfMemoryError: Java heap space ------> java heap overflow, in which case the most common, usually due to a memory leak or heap size caused by improper settings. For memory leaks, leaks need to find the code in the program memory by monitoring software, and heap size can -Xms, -Xmx such as modified by the virtual machine parameters.
- java.lang.OutOfMemoryError: PermGen space ------> java permanent generation of an overflow, i.e. the overflow method, typically occurs in large or Class jsp pages, or the case of using reflection cglib the like, since the above situation will produce a large Class information is stored in the method area. Such a situation can be resolved by changing the method area size, use similar -XX: PermSize = 64m -XX: MaxPermSize = 256m form of modification. Further, particularly excessive constant string will cause the method to overflow.
- java.lang.StackOverflowError ------> not throw OOM error, but it is also more common Java memory overflow. JAVA virtual machine stack overflow, generally due to an infinite loop or the depth of recursive calls caused by the presence of the program, stack size is too small there will be such a spill. You can set the stack size by a virtual machine parameters -Xss.
- Set JVM parameters -XX: + HeapDumpOnOutOfMemoryError, OOM occurs automatically set when the dump heap information. However, this method requires JDK5 above.
- Use JDK comes jmap command. "Jmap -dump: format = b, file = heap.bin <pid>" which can be obtained by JPS pid.
- mat: eclipse memory analyzer, memory analysis tool based on eclipse RCP's. For details, see: http: //www.eclipse.org/mat/, recommended.
- jhat: JDK comes with java heap analyze tool, you can stack objects are displayed in the form of html out, including the number of objects, size, etc., and supports Object Query Language OQL, analysis related applications, you can http: // localhost: 7000 to access the results. Not recommended, because in the actual investigation process, generally the first in a production environment to dump a file, then pulled on his analysis of the development of the machine, so, as the use of advanced analysis tools such as the front of the mat to the efficient.