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

How to answer and solve OOM problems

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

In this issue, the editor will bring you about how to answer and solve OOM questions. 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.

The last time I went out to paddle, I was asked how to quickly solve the online OOM problem, because my vegetable chicken's answer was not very good, so I came back to consult the data and consult other bosses to sum up.

First, why OOM

There are mainly the following two points:

1. Less memory is allocated: for example, the memory available to the virtual machine itself (usually specified by the VM parameter at startup) is too small.

2. Too many applications are used, and they are not released when they are used up, which is wasted. This will cause a memory leak or a memory overflow.

Memory leak: the applied memory is not released, so that the virtual machine cannot use the memory again, and the memory is leaked because the applicant does not use it and cannot be allocated to someone else by the virtual machine.

Memory overflow: the requested memory exceeds the amount of memory that JVM can provide, which is called an overflow.

2. What are the types of OOM

According to the JVM specification, OOM may be thrown from every memory region except that the program counter does not throw OOM.

The JAVA virtual machine manages the following memory areas at run time:

1. Program counter: the line number indicator of the bytecode executed by the current thread, thread private.

2. JAVA virtual machine stack: the memory model executed by the Java method. The execution of each Java method corresponds to the operation of entering and leaving the stack frame.

3. Local method stack: similar to the "JAVA virtual machine stack", but provides a memory environment for the operation of the native method.

4. JAVA heap: the place where object memory is allocated, the main area of memory garbage collection, shared by all threads. Can be divided into the new generation, the old generation.

5. Method area: used to store class information, constants, static variables, code compiled by just-in-time compiler and other data that have been loaded by JVM. The eternal generation in Hotspot.

6. running constant pool: part of the method area that stores constant information, such as various literals, symbolic references, etc.

7. Direct memory: memory that can be accessed directly, such as NIO, is not part of the JVM runtime data area.

The three most common OOM situations are as follows:

1 、 java.lang.OutOfMemoryError: Java heap spacejava

Heap memory overflows, which are the most common, are usually caused by memory leaks or improper heap size settings. For memory leaks, you need to find the leaked code in the program through the memory monitoring software, and the heap size can be modified by the virtual machine parameter-Xms,-Xmx, etc.

2 、 java.lang.OutOfMemoryError: PermGen spacejava

Permanent generation overflow, that is, method area overflow, usually occurs in a large number of Class or jsp pages, or in the case of reflection mechanisms such as cglib, because a large amount of Class information is stored in the method area. This situation can be solved by changing the size of the method area, using a form similar to-XX:PermSize=64m-XX:MaxPermSize=256m. In addition, too many constants, especially strings, can also cause the method area to overflow.

3. Java.lang.StackOverflowError, OOM error will not be thrown, but it is also a common Java memory overflow. JAVA virtual machine stack overflow is usually caused by endless loops or deep recursive calls in the program, which can also occur when the stack size setting is too small. The stack size can be set by the virtual machine parameter-Xss.

Third, how to analyze the OOM problem

To analyze OOM, we must obtain a memory image of the dump heap.

There are two ways to do this:

1. Set JVM parameters

-XX:+HeapDumpOnOutOfMemoryError, setting the dump out-of-heap information automatically when OOM occurs. However, this method requires a later version of JDK5.

2. Use the jmap command that comes with JDK. "jmap-dump:format=b,file=heap.bin" where pid can be obtained through jps. After the dump heap memory information, you need to analyze the files out of dump to find out the cause of OOM. Common tools are:

① mat:eclipse memory analyzer, a memory analysis tool based on eclipse RCP. For more information, please see http://www.eclipse.org/mat/, recommended.

② jhat:JDK comes with java heap analyze tool, which can display the objects in the heap in the form of html, including the number and size of objects, and supports object query language OQL. After analyzing related applications, you can access the analysis results through http://localhost:7000. It is not recommended, because in the actual troubleshooting process, it is generally necessary to dump files in the production environment, and then pull them to your own development machine for analysis, so it is better to use advanced analysis tools, such as the previous mat, to be efficient.

This link online: an example of using mat analysis is provided in https://0x9.me/c9jS1.

Note: because the JVM specification does not define the format of files exported by dump, the dump files generated by different virtual machines are not the same. When analyzing, you need to use different analysis tools for the output of different virtual machines (of course, some tools can be compatible with multiple virtual machine formats). IBM HeapAnalyzer is also a common tool for analyzing heap.

The above is the editor for you to share how to answer and solve OOM problems, if you happen to have similar doubts, you might as well 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

Internet Technology

Wechat

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

12
Report