JAVA programs run on virtual machines and require memory space when running. When a virtual machine executes a JAVA program, the memory it manages is divided into different data areas for easy management. The virtual machine management memory data area is divided into the following figure:
Java learning video recommendation: java online tutorial
1. Program Counter Register
Line number indicator, branch, loop, jump, exception handling, thread recovery (CPU switching) of bytecode instructions, each thread requires an independent The counter and thread private memory do not affect each other, and no memory overflow exception will occur in this area.
2. Virtual Machine Stack (VM Stack)
The virtual machine stack (VM Stack) is private to the thread. The declaration period is the same as the thread. The virtual machine stack is Java Memory model of method execution. When each method is executed, a stack frame is created, which is the basic data structure during method execution.
The stack frame is used to store: local variable table, operand stack, dynamic link, method exit, etc. The execution of each method corresponds to the process of the virtual machine stack frame from the stack to the stack. It is a data structure, a local variable table in the virtual machine, corresponding to the program data model above the physical layer.
The local variable table is a program running data model that stores various data types known during compilation, such as:
Boolean, byte, char, short, int, float, long, Double, object reference type (object memory address variable, pointer or handle). When the program is running, the stack frame space size is allocated according to the local variable table. During operation, the size is unchanged. Exception type: stackOverFlowError. The thread request stack depth is greater than the virtual machine allowed depth. OutOfMemory
The memory space is exhausted and cannot be expanded.
3. Native Method Stack
Similar to the virtual machine stack, the virtual machine stack serves Java programs, and the local method stack supports the running of the virtual machine The specific implementation of the service is determined by the virtual machine manufacturer and will also throw stackOverFlowError
and OutOfMemory
exceptions.
4. Heap
The Heap is the largest part of the memory managed by the virtual machine. It is shared by all threads and is used to store object instances (objects). , array), physically discontinuous memory space. Since the GC collector collects in generations, it is divided into: new generation Eden, From SurVivor space, To SurVivor space, allot buffer (allocation space), which may be divided into multiple Thread-private buffer, old generation.
5. Method Area
The method area (Method Area), like the heap area, is a thread-shared memory area and is used to store data loaded by the virtual machine. Class information, constants, static variables, code compiled by the just-in-time compiler (dynamically loaded OSGI) and other data. Theoretically, it is part of the Java virtual machine. To distinguish it, it is called Non-Heap.
You can choose not to perform garbage collection in this area. The purpose of recycling in this area is mainly to recycle the constant pool and uninstall classes. When the memory area is insufficient, an OutOfMemory exception will be thrown.
Runtime constant pool: part of the method area, Class version, fields, interfaces, methods, etc., as well as various literals and symbol references generated during compilation, which are stored in this area after the compiled class is loaded. OutOfMemory exception will be thrown.
6. Direct Memory
Direct Memory does not belong to the virtual memory area. It is an IO method based on channels and buffers. You can use native functions to directly allocate off-heap memory, store the referenced external memory address in the heap, and complete the operation of directly referenced memory through references. The NIO provided after 1.4 significantly improves efficiency and avoids the back-and-forth copy operation between heap memory and Native memory. , not controlled by the virtual machine memory, will throw an OUTOfMemory exception.
Recommended related article tutorials: Getting started with java development
The above is the detailed content of An in-depth introduction to the JVM memory data area. For more information, please follow other related articles on the PHP Chinese website!