Frequent interviews-various situations of JVM out of memory (OOM)

Stack overflow

The stack size in the HotSpot version is fixed and does not support expansion.

Java.lang.StackOverflowError (single virtual machine stack) is very difficult for ordinary method calls to appear. If encountered, infinite recursion may be written .

The enlightenment that the virtual machine stack brings us: The execution of the method is packaged into a stack frame. So it is inherently slower than a simple process-oriented loop. Therefore, both recursive and non-recursive (not encapsulated, realized by direct loop) in the tree traversal algorithm have their meanings. Recursive code is more concise, non-recursive code is copied but faster.

The OOM of the stack memory (here refers to the stack space of the entire runtime data area) occurs under the condition that threads are constantly created, and the JVM will continuously apply for stack memory from the operating system. If the machine does not have enough memory, it will directly crash. .

 

Note: (1) In the Java language, when a thread is created, the virtual machine creates a Thread object in the JVM memory and at the same time creates an operating system thread. The memory of this operating system thread is not JVMMemory, but the rest of the system Memory (direct memory, off-heap memory)

 

Extension: (2) When you use JAVA threads, a Thread object will be created in the JVM. But at the same time, a real physical thread will be created in the operating system (refer to the JVM specification), and the operating system will create this physical thread in the remaining memory instead of in the JVM. Therefore, if you want to create more threads, you must reserve sufficient off-heap memory.

image.png

image.png

 

Heap overflow (memory focus)

Memory overflow: The requested memory space exceeds the maximum heap memory space.

If it is not a program problem, but our program does have a lot of memory and the JVM cannot satisfy us, we can solve it by adjusting the heap memory parameters.

Memory leak: Refers to an object that has not been used for a long time but has been unable to be recycled.

If it is a memory leak, but the objects stored in the heap must be alive (very low usage), then you should check the heap parameter settings of the JVM, which space is adjustable compared with the machine memory. Then check from the code whether there are some object life cycles that are too long, the storage structure design is unreasonable, etc., to minimize the memory consumption when the program is running.

 

Method area overflow

(1) Runtime constant pool overflow (in the permanent generation before JDK6, the constant pool will overflow. With the permanent generation replaced by meta space, our method area constant pool moved to the heap memory. Therefore, the constant pool overflows The situation is rare)

(2) The Class object saved in the method area is not recycled in time, or the storage occupied by the Class information exceeds our configuration.

  Note: The conditions for recycling classes are very harsh.

1. All instances of this class have been recycled, that is, there is no longer any instance of this class in the heap memory

2. Record that the ClassLoader of this class has been recycled

3. The java.lang.Class object corresponding to this class is not referenced anywhere, and the method of this class cannot be accessed through reflection anywhere.

 

Native memory overflow

Direct memory is not a memory area defined in the JAVA virtual machine specification. The NIO class was newly added in JDK1.4, and a channel and buffer-based I/O method was introduced. The native function library can be used to directly allocate off-heap memory (the work of the local method stack), and then store it in The DirectByteBuffer object in the JAVA heap operates on this memory reference. This can significantly improve performance in some scenarios and avoid copying data back and forth between the Java heap and the Native heap.

1. The direct memory allocation of this machine will not be limited by the Java heap size, and the total memory size of the receiving machine.

2. The size of direct memory can be set with parameters

3. Direct memory application space consumes higher performance

4. The performance of direct memory IO read and write is better than ordinary memory

 

When we need to frequently access large memory instead of requesting and releasing space, performance can be improved by using direct memory.

 

Use the UnSafe class to continuously apply for space for direct memory, and eventually OOM.

 

Note: Since the application for direct memory is not managed by the virtual machine, the resulting OOM will not be obvious in the Heap Dump file. When it is found after OOM that the Dump file is very small and the program uses NIO directly or indirectly, you can consider whether it is a direct memory overflow.

 

Unsafe

The classes that manipulate direct memory in Java are no longer used after JDK1.9.

Guess you like

Origin blog.csdn.net/weixin_47184173/article/details/113576943