1. OOM for heap=> example: Java.lang.OutOfMemoryError:Java heapspace
Analysis
This oom is due to the fact that the maximum heap value in the JVM does not meet the needs, the maximum value of the set heap is increased, the sample parameter is:-xmx2g
"Workaround"
Increase the maximum value of the heap, that is, the value of the-XMX is increased.
2. OOM for perm=> example: Java.lang.OutOfMemoryError:Java permspace
Analysis
This oom is due to the maximum value of perm in the JVM does not meet the need, set the maximum value of perm can be increased, the parameters sample is:-xx:maxpermsize=512m
"Workaround"
Increase the maximum value of the heap, that is, the value of the-xx:maxpermsize is increased.
Also, note that perm typically loads classes when the JVM is started, and if the JVM is running for a long time instead of just after it has been started,
It is likely that the runtime has a class that is dynamically loaded, and it is recommended that you uninstall the configuration with the classes in the CMS policy.
such as:-xx:+useconcmarksweepgc-xx:+cmsclassunloadingenabled
3. OOM for gc=> example: Java.lang.OutOfMemoryError:GC overheadlimit exceeded
Analysis
This oom is due to the fact that the JVM has too many objects in the GC to cause memory overflow, it is recommended to adjust the GC's policy to start the GC at a certain scale instead of using the default policy, or to set the new generation and the old generation to the appropriate size,
Fine-tuning survival is required.
"Workaround"
Changing the GC policy, when the older generation is 80%, starts the GC and sets-xx:survivorratio (-xx:survivorratio=8) and-xx:newratio (-xx:newratio=4) more reasonable.
4. OOM for Native thread created=> example: java.lang.OutOfMemoryError:unable tocreate new native thread
Analysis
Refer to the following:
(maxprocessmemory-jvmmemory-reservedosmemory)/(threadstacksize) = Numberof threads
Maxprocessmemory refers to the maximum memory of a process
Jvmmemory JVM Memory
Reservedosmemory reserved Operating system memory
Size of the Threadstacksize line stacks
If the JVM memory is too large or the utilization rate is less than 20%, it is recommended to lower the maximum heap and perm, and the line stacks small, that is,-XSS small, such as:-xss128k
"Workaround"
Under the premise that the JVM memory cannot be reduced, the-XSS setting is small, for example:-xss:128k
5. OOM for allocate huge array=> example: Exception in Thread "main": java.lang.OutOfMemoryError:Requested array size exceeds V M limit
Analysis
This information indicates that the application (or APIs called by the application) attempted to allocate an array larger than the heap size. For example, if the application is new to an array object with a size of 512M, but the maximum heap size is 256M, OutOfMemoryError is thrown because the size of the array exceeds the limit of the virtual machine.
"Workaround"
(1), first check the heap-xmx is not set too small
(2), if the heap-xmx is already large enough, then check the application is not a bug, for example: The application may be in the calculation of the size of the array, there is an algorithm error, resulting in a large array size, resulting in a large array is allocated.
6. OOM for small swap=> example: Exception in Thread "main": Java.lang.OutOfMemoryError:request <size> bytes for <rea Son>. Out of swap space?
Analysis
This type of error is thrown due to a failure to allocate memory from the native heap, and the heap memory may be nearly exhausted. This type of error may not be related to the application, such as the following two reasons that can also cause errors to occur:
(1) The operating system is configured with a smaller swap area
(2) Another process in the system is consuming all the memory
"Workaround"
(1), check the OS swap is not set or set too small
(2), check if there are other processes consuming large amounts of memory, resulting in insufficient allocation of current JVM memory.
Note: Although sometimes the <reason> section shows the cause of oom, but most of the time,<reason> shows the name of the source module that prompted the assignment to fail, it is necessary to view the log file, such as the HS file when crash.
[Reprint] Scenarios and workarounds for Oom in Java