Reproduced from the original text:
- What is Java OOM? How to analyze and solve oom problem?
What is Oom?
OOM, the full name of "Out of Memory", translated into Chinese is "exhausted", the form of expression is "java.lang.OutOfMemoryError". Abnormal. Take a look at the official note:
Thrown when the Java Vsan cannot allocate an object because it was out of memory, and no further memory could be MA De available by the garbage collector.
Translation: This exception is thrown when memory is exhausted and no recyclable space is available.
Why is Oom?
Why is there no memory? There are two reasons why:
- Less: For example, the memory that the virtual machine itself can use (typically executed by the VM parameters at startup) is too small.
- Use more: should be useful too much, and run out of no release, causing a memory leak or memory overflow.
Memory leaks
The memory that was requested is not released, causing the virtual machine to not use the memory again, and the memory is leaked because the requester is not available and cannot be assigned to another user by the virtual machine.
Memory overflow:
The requested memory exceeds the amount of memory that the JVM can provide, which is called overflow at this time.
In the days before garbage collection, such as C and C + +, we had to personally take care of the memory application and release operation, if we applied for memory, then forgot to release it, such as new in C + + but without delete, it could cause memory leak. Occasional memory leaks may not cause problems, and a large amount of memory leaks can cause memory overruns.
In the Java language, due to the existence of automatic garbage collection mechanism, so we generally do not have to actively release the memory of the unused objects, that is, theoretically, there will be no "memory leaks." However, if the encoding is inappropriate, for example, a reference to an object is placed in a global map, although the method is finished, the garbage collector reclaims the memory based on the object's reference, which prevents the object from being reclaimed in a timely manner. If this occurs more often, it can lead to memory overflow, such as the caching mechanism that is used frequently in the system. Memory leaks in Java, unlike the forgotten delete in C + +, are often logical causes of leaks.
Common Oom Types
The most common Oom scenarios are the following three types:
java.lang.OutOfMemoryError: Java heap space: Java heap memory overflow, which is most commonly caused by a memory leak or improper heap size setting. For a memory leak, the leak code in the program needs to be looked up through the memory monitoring software, and the heap size can be modified by the virtual machine parameters -Xms and me -Xmx .
java.lang.OutOfMemoryError:PermGen space: Java persistent overflow, that is, the method area overflow, the general emergence of a large number of class or JSP pages, or the use of cglib and other reflection mechanism, because the above situation will generate a lot of class information stored in the method area. This situation can be resolved by changing the size of the method area and modifying it in -XX:PermSize=64m -XX:MaxPermSize=256m a similar form. In addition, too many constants, especially strings, can cause the method area to overflow.
java.lang.StackOverflowError: Oom Error is not thrown, but it is also a more common Java memory overflow. Java Virtual machine stack Overflow, usually caused by the existence of a dead loop or deep recursive call in the program, the stack size is too small to appear this kind of overflow. -Xssthe size of the stack can be set through the virtual machine parameters.
Oom Analysis---Heapdump
- Generate dump File:
jmap -dump:format=b,file=heap.bin <bin> where PID can be obtained by JPS.
- Using Professional analysis tools:
- Mat (Eclipse Memory Analyzer Tool), based on Eclipse RCP's memory analysis tools.
- Jhat:jdk
java heap analyze tool , the objects in the heap can be displayed in HTML, including the number of objects, size, and so on, and support the object query language OQL, after analyzing the relevant application, you can http://localhost:7000 access the analysis results. Not recommended, because in the actual troubleshooting process, the general is to dump files in the production environment, and then pull to their own development machine analysis, so, instead of using advanced analysis tools such as the previous mat to efficient.
This link: http://www.ibm.com/developerworks/cn/opensource/os-cn-ecl-ma/index.html provides an example of using mat analysis.
Note: Because the JVM specification does not define the format of the dump file, the dump file generated by the different virtual machines is not the same. At the time of analysis, different analysis tools are required for the output of different virtual machines (of course, some tools are compatible with multiple virtual machine formats). IBM Heapanalyzer is also a common tool for analyzing the heap.
Summary
The technology or tools involved in virtual machines often need to take into account the virtual machine specification as well as the different virtual machine implementations. Especially for virtual machine tuning, it is often necessary to consider the virtual machine in some aspects of the implementation strategy, for example, different virtual machine garbage collection algorithm is not the same, which directly affects the virtual machine some parameters of the settings to achieve the best performance of the virtual machine.
And for the JVM runtime analysis and diagnosis, you need to master the analysis of the basic methods, specific circumstances, the use of virtual machine principles, specific analysis. A word, the water is very deep ah.
Java Oom Learning