Deep understanding of JVM memory zone and memory overflow
Document directory
1.2. HotSpot Virtual Machine
1.3. troubleshooting of OOM exceptions
1.4. Reference
Java memory overflow exception
Data Area During Running
Program counters
-
Row number indicator of the bytecode executed by the current thread
-
The current thread is private
-
There will be no OutOfMemoryError
Java Virtual Machine Stack
-
The thread is private and has the same lifecycle as the thread.
-
Memory Model for java method execution. A stack frame is created when each method is executed to store information such as the local variable table (basic type, Object Reference), operand stack, dynamic link, and method exit.
-
StackOverflowError exception: When the stack depth requested by the thread is greater than the depth allowed by the Virtual Machine
-
OutOfMemoryError exception: If the stack cannot be extended, enough memory is applied.
Local method Stack
Similar to the Virtual Machine stack, the Native method service is mainly used by virtual machines. In the HotSpot virtual machine, the local method stack is directly integrated with the Virtual Machine stack.
Java Heap)
Java heap is a memory area shared by all threads. It is created when the VM is started. The only purpose of this region is to store object instances. Java heap is the main area for managing the garbage collector. Java heap can also be subdivided into the new generation and old generation. The details include Eden space, Form compute vor space, and To compute vor space.
Method Area
-
Thread sharing
-
Stores information about classes loaded by virtual machines, constants, static variables, Code Compiled by the real-time compiler, and other data.
-
OutOfMemoryError exception: when the method area cannot meet the memory allocation requirements
Runtime constant pool
Direct Memory
-
NIO can use the Native function library to directly allocate off-heap memory. The DirectByteBuffer object in the heap is used as a reference for this memory.
-
The Java heap size is not limited by the local (server) memory size.
-
OutOfMemoryError exception: when the system memory is insufficient
HotSpot Virtual Machine
Object Creation
When the VM encounters a new command, it first checks whether the parameter of this object is located in the constant pool as a symbol reference of a class, check whether the class referenced by this symbol has been loaded, parsed, and initialized. If no, the class loading process must be performed first.
After the class loading check is passed, the VM allocates memory for the new object. The memory size required by the object can be determined after the class is loaded. Memory allocation can be implemented by means of "pointer Collision" and "Idle list.
Object Access and positioning
Java programs need to use the reference data on the stack to operate on specific objects on the stack. There are two access methods: handle and direct pointer.
-
When a handle accesses the java heap, a memory block is allocated as the handle pool. The reference stores the handle address of the object, the handle contains the specific address information of the object instance data and type data.
-
In the layout of direct pointer access to java heap objects, you must consider how to place information related to access type data. The object address stored in reference is the object address.
Troubleshooting of OOM exceptions
Generate a Dump snapshot file:
-
The jvm parameter-XX:-HeapDumpOnOutOfMemoryError allows the JVM to Dump the current memory Dump snapshot when memory overflow occurs.
-
Use jmap to generate dump files, win view the tomcat process pid through the task manager, linux use the ps command to view the process pid, and then use the jmap command
First, use the Memory Image Analysis Tool (such as the Memory Analyzer of Eclipse) for analysis. common situations include:
-
Memory leakage, the object is dead, and automatic recycle cannot be performed through the garbage collector. The solution can be determined by finding out the location and cause of the leaked code;
-
Memory overflow. All objects in the memory must be stored. This indicates that the Java Heap has insufficient space to allocate. Check the heap settings-Xmx and-Xms ), check whether the Code contains a situation where the object's lifecycle is too long and its holding status is too long.
OOM exception example:
Package oom;
- import java.util.ArrayList;
- import java.util.List;
-
- /**
- * VM Args: -Xms20m -Xmx20m -XX:+HeapDumpOnOutOfMemoryError
- * @ClassName: HeapOOM
- *
- */
- public class HeapOOM {
- static class OOMObject{
-
- }
-
- public static void main(String[] args) {
- List<OOMObject> list = new ArrayList<OOMObject>();
- while(true){
- list.add(new OOMObject());
- }
- }
- }