When a serious shortage of the JVM memory, will be thrown. Java lang. An OutOfMemoryError. This article summarizes common OOM causes and solutions, as shown in the figure below. If there are omissions or mistakes, welcome to add corrections.
Java Heap Space
When the Heap memory (Heap Space) does not have enough Space to store the newly created object, will be thrown. Java lang. OutOfMemoryError: Javaheap Space error (according to the actual production experience, You can configure a keyword alarm for OutOfMemoryError in the program log and handle it immediately if it is found.
Cause analysis,
Common causes of Javaheap space errors fall into the following categories:
Request to create a very large object, usually a large array.
2, exceeding the expected volume of visits/data, usually the upstream system request traffic surge, common in all kinds of promotion/kill activities, can be combined with the business traffic indicators to check whether there is a spike.
3. Excessive use of finalizers. The object is not immediately GC.
In a Memory Leak, a large number of object references are not released and cannot be automatically recycled by the JVM.
The solution
For most cases, it is usually just a matter of scaling up the JVM heap memory space with the -xmx parameter. If the problem persists, refer to the following conditions for further handling:
1. If it is a large object, you can check its rationality, for example, whether all the results of the database are queried at one time, and there is no limit on the number of results.
2. If the service is under peak pressure, add machine resources or perform traffic limiting degradation.
3. If there is a memory leak, you need to find the holding object and modify the code design, such as closing the unfreed connection.
2, GC overhead limit exceeded
When Java process takes more than 98% of the time doing GC, but back to less than 2% of the memory, and repeated five times in a row, the activity will be thrown. Java lang. OutOfMemoryError: GC overhead limit exceeded the error. Simply put, the application has almost exhausted all available memory and the GC cannot reclaim it.
The causes and solutions for this kind of problem are very similar to Javaheap space, as mentioned above.
3, Permgen space
This error indicates that the Permanent Generation is full, usually because too many classes are loaded or too large.
Cause analysis,
Permanent generation storage objects include the following types:
Load/cache class definitions into memory, including class names, fields, methods, and bytecodes;
2. Constant pool;
The class associated with an array of objects/types.
4. JIT compiler optimized class information.
The amount of PermGen used is positively correlated with the number/size of classes loaded into memory.
The solution
Depending on the timing of the Permgen Space error, different solutions are available, as follows:
1, program startup error, modify -xx :MaxPermSize startup parameter, increase the permanent generation space.
2, Application redeployment error, most likely there is no application did not restart, resulting in the loading of multiple class information, just need to restart the JVM to resolve the problem.
An application may create a large number of classes that have a short lifetime, but the JVM does not uninstall classes by default. The -xx :+CMSClassUnloadingEnabled and -xx :+UseConcMarkSweepGC parameters can be set to allow the JVM to unload the class.
Jmap-dump :format=b,file=dump.hprof
4, Metaspace
JDK 1.8 used Metaspace instead of Permanent Generation. This error indicates that Metaspace is full, usually because of too many classes loaded or too large.
The causes and solutions to these problems are very similar to Permgenspace, as mentioned above. Note that the startup parameter to adjust the Metaspace space size is -xx :MaxMetaspaceSize.
5, Unable to create new native thread
Each Java thread consumes a certain amount of memory, and this error is reported when the JVM requests the underlying operating system to create a new native thread if there are not enough resources to allocate.
Cause analysis,
When a JVM fails to request the OS to create a nativethread, it will throw an Unableto CreatenewnativeThread. Common reasons include:
1. The number of threads exceeds the ulimit limit.
2. The number of threads exceeds kernel.pid_max (restart only).
3. Insufficient native memory;
The common process of this problem mainly includes the following steps:
1. An application within the JVM requests the creation of a new Java thread.
2. The JVM Native method proxies this request and requests the operating system to create a native thread.
3. The operating system tries to create a new native thread and allocate memory for it;
4. If the virtual memory of the operating system is exhausted, or the address space of the 32-bit process is limited, the operating system will reject this native memory allocation.
5, the JVM will throw Java. Lang. OutOfMemoryError: Unableto createnewnativethread errors.
The solution
1. Upgrade the configuration to provide more memory for the machine;
2. Reduce the Java Heap Space size;
3, fix the application thread leakage problem;
4. Limit thread pool size.
Use the -xss argument to reduce the size of the thread stack.
6. Increase the maximum number of threads on the OS layer: Run ulimia-a to check the maximum number of threads, and run ulimit-u XXX to adjust the maximum number of threads.
ulimit -a …. ….. max user processes (-u) 16384
What other problems do you have? What are the solutions to your problems? Welcome message points out to communicate together.