Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the scenario and solution of OOM in Java

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about the scene and solution of OOM in Java. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

1. OOM for Heap= > for example: java.lang.OutOfMemoryError: Java heap space

Separation and analysis

This OOM is due to the fact that the * value of heap in JVM does not meet the needs. You can set the * value of heap higher. The parameter example is:-Xmx2G.

Solution method

Increase the * * value of heap, that is, the value of-Xmx. 2. OOM for Perm= > for example: java.lang.OutOfMemoryError: Java perm space

Separation and analysis

This OOM is due to the fact that the * value of perm in JVM does not meet the needs. You can set the * value of perm higher. The parameter example is:-XX:MaxPermSize=512M.

Solution method

Increase the * * value of heap, that is, the value of-XX:MaxPermSize.

In addition, note that Perm usually loads classes when JVM starts. If JVM runs for a long time instead of overflowing just after startup, it is probably because classes are loaded dynamically at runtime. It is recommended to use the classes in CMS policy to unload the configuration.

For example:-XX:+UseConcMarkSweepGC-XX:+CMSClassUnloadingEnabled3, OOM for GC= > for example: java.lang.OutOfMemoryError: GC overhead limit exceeded

Separation and analysis

This OOM is due to memory overflow caused by too many objects in JVM when GC. It is recommended to adjust the policy of GC, start GC in a certain proportion instead of using the default policy, or set the new generation and the old generation to an appropriate size, and you need to fine-tune the survival rate.

Solution method

Change the GC policy to start GC at 80% of the older generation, and set-XX:SurvivorRatio (- XX:SurvivorRatio=8) and-XX:NewRatio (- XX:NewRatio=4) more reasonably. 4. OOM for native thread created= >

Such as: java.lang.OutOfMemoryError: unable to create new native thread

Separation and analysis

References are as follows:

(MaxProcessMemory-JVMMemory-ReservedOsMemory) / (ThreadStackSize) = Number of threads

MaxProcessMemory refers to the memory of a process.

JVMMemory JVM memory

Operating system memory reserved by ReservedOsMemory

The size of the ThreadStackSize thread stack

If the JVM memory is too large or the availability is less than 20%, it is recommended to lower the * values of heap and perm, and reduce the thread stack, that is,-Xss, such as-Xss128k.

Solution method

Under the premise that the JVM memory cannot be reduced, set the-Xss to a small value, such as-Xss:128k

5. OOM for allocate huge array= > for example: Exception in thread "main": java.lang.OutOfMemoryError: Requested array size exceeds VM limit

Separation and analysis

This kind of information indicates that the application (or the APIs called by the application) is trying to allocate an array larger than the heap size. For example, if your application new an array object with a size of 512m but a heap size of 256m, OutOfMemoryError will throw because the size of the array exceeds the limit of the virtual machine.

Solution method

(1) first check whether the-Xmx of heap is set too small.

(2) if the-Xmx of heap is large enough, check whether the bug exists in the application. For example, the application may have an algorithm error when calculating the size of the array, resulting in a large size of the array, resulting in a large array being allocated.

6. OOM for small swap= > for example: Exception in thread "main": java.lang.OutOfMemoryError: request bytes for. Out of swap space?

Separation and analysis

Such errors are thrown because of a failure to allocate memory from the native heap and the heap memory may be nearly exhausted. This type of error may have nothing to do with the application, for example, the following two reasons can also lead to errors:

(1) the operating system is configured with a smaller switching area.

(2) another process in the system is consuming all the memory.

Solution method

(1) check whether the swap of os is not set or is too small.

(2) check whether other processes are consuming a lot of memory, resulting in insufficient allocation of current JVM memory.

Note: although the cause of OOM is sometimes partially displayed, in most cases, the name of the source module that prompts for a failed allocation is displayed, so it is necessary to look at the log file, such as the hs file when crash.

These are the scenarios and solutions of OOM in Java shared by Xiaobian. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report