What is java OOM? How to analyze and solve problems oom? [Reserved]

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).

 
2) Why OOM?
 
Why no memory of it? There are two reasons for no more than:
 
1) allocated less: the virtual machine itself can be used such as memory (generally designated by the start VM parameter) too.
 
2) application with too much, and did not release exhausted, wasted. At this time, it will cause a memory leak or memory overflow.
 
Memory leaks : Applicants are finished using the memory is not released, resulting in the virtual machine can not use the memory again, this time on this memory leak, because the applicants do not have, and who can not be assigned to a virtual machine with others.
Out of memory : memory allocated memory size exceeds the JVM can provide, at this time is called overflow.
 
In the absence of automatic garbage collection days, such as C and C ++ language, we must personally responsible for memory application and release operation, if the application memory, run out and then forget the release, such as C ++ in the new up but did not delete , then it may cause a memory leak. Occasional memory leak may not cause problems, but a lot of memory leaks may cause memory overflow.
 
In the Java language, because of the automatic garbage collection mechanism, so we generally do not take the initiative to release the memory occupied by objects, that is, in theory, would not exist "memory leak" of. However, if improperly coded, for example, a reference to an object into a global Map, although the method is over, but because the garbage collector to reclaim the memory according to citations object, causing the object can not be recovered in time. If the case occurs several times, it will cause a memory leak, such as caching system frequently used. Memory leaks in Java, unlike C ++, forget delete, often the cause of leaks on logic.
 
3) OOM type
 
JVM memory model:
 
According to the JVM specification, JAVA virtual machine at runtime memory management of the following areas:
  • 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.
According to the JVM specification, in addition to the program counter does not throw OOM, but various other memory areas are likely to throw OOM.
 
The most common OOM situation following three:
  • 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.
4) OOM analysis --heapdump
 
To dump heap memory mirroring can be used in two ways:
  • 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.
After the dump heap memory information necessary to analyze the dump file out to find the cause of the OOM. Commonly used tools are:
  • 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.
This link: http: //www.ibm.com/developerworks/cn/opensource/os-cn-ecl-ma/index.html provides an example of using a mat analysis.
 
Note: Because the JVM specification does not define the dump out the file format, so dump files produced by different virtual machines are not the same. On analysis, the need to use different tools for analyzing the output of different virtual machines (Of course, some tools format compatible with multiple virtual machines). IBM HeapAnalyzer analysis is a common tool of the heap.
 
5) Summary
 
Related to technology or tool of virtual machines, often need to consider virtual machine specification as well as different virtual machine implementations. Especially for the virtual machine tuning is often required for virtual machines in some areas of implementation strategies to consider, for example, different garbage collection algorithm of the virtual machine is not the same, and this directly affects certain parameters of the virtual machine settings to achieve the best performance of the virtual machine.
And for analysis and diagnosis JVM run-time, you need to master the basic analysis methods for the specific circumstances, using the principle of virtual machines, specific analysis. In short, the water deep ah.

Guess you like

Origin www.cnblogs.com/chaos-li/p/11140065.html