JVM virtual machine's virtual machine area division and content (1)

Data area at runtime

The Java virtual machine divides the managed memory into several areas when executing Java programs

Reprint
Method area and heap area are thread shared data area, virtual machine stack, local method stack, and program counter are private to thread.

Program counter

The program counter is a small memory space. It can be seen as an indicator of the line number of the bytecode executed by the current thread. The bytecode interpreter selects the next bytecode instruction to be executed by changing the value of the counter. When multithreading, each thread has an independent program counter, and each counter does not affect each other.
If the thread is executing a Java method, the counter records the address of the virtual machine bytecode instruction being executed. When the local method is executed, the counter is empty (Undefined),
which is an indicator of program control flow. Basic functions such as branch, loop, jump, exception handling, and thread recovery all need to rely on this counter to complete

Java virtual machine stack

The life cycle is the same as the thread. The virtual machine stack describes the memory model of Java method execution: when each method is executed, the Java virtual machine will synchronously create a stack frame to store the local variable table, operand stack, dynamic link, method Export and other information . The process of each method being called until the completion of execution corresponds to the process of a stack frame from the virtual machine stack to the stack.
The local variable table stores the basic data types of the Java virtual machine (boolean, byte, char, short, int, float, long, double) that can be known at compile time, and object references (not equal to the object itself, it may be a pointer to the start of the object) The reference pointer of the address may also be a handle representing the object or other locations related to this object), returnAddress (pointing to the address of a bytecode instruction).
The storage space of these data types in the local variable table is represented by a local variable slot (slot), where 64-bit long and double data will occupy two variable slots, and the rest will only occupy one.
The memory space required by the local variable table is allocated during compilation. The memory allocated when entering the method is determined, and the size of the local variable table will not be changed during operation (the size refers to the number of slots)
if the thread request depth is greater than the virtual machine allows If the Java virtual machine stack capacity can be dynamically expanded, when the stack is expanded, it will throw an OutMemoryError exception if it cannot apply for enough memory.

Native method stack

The virtual machine stack serves to execute Java methods (bytecode), and the local method stack serves for the virtual machine to execute local methods.

Java heap

The largest piece of memory managed by the virtual machine is shared by all threads. It is created when the virtual machine starts. The sole purpose of this area is to store object instances.
From the perspective of memory allocation, the Java heap can be divided into multiple thread-private allocation buffers (TLAB) to improve the efficiency of object allocation.
When the stack cannot be expanded, the virtual machine throws OutMemoryError exception.

Method area

The thread shared area is used to store the type information loaded by the virtual machine, constants, static variables, and code cache compiled by the just-in-time compiler.
When the method area cannot meet the new memory allocation requirements, an OutOfMemoryError exception will be thrown.

Runtime constant pool

The runtime constant pool is a part of the method area. In addition to the description information of the class version, fields, methods, interfaces, etc., the Class file also has a constant information table for storing various literals and symbol references generated during compilation. This part of the content will be stored in the runtime constant pool of the method area after the class is loaded.
The constant pool will throw OutOfMemoryError when it cannot apply for memory.

Direct memory

Direct memory is not part of the runtime data area of ​​the Java virtual machine.
The NIO (New Input/Output) class was newly added in JDK1.4, and the channel and buffer-based I/O method was introduced. It can use the Native function library to directly allocate external memory, and then pass a DirectByteBuffer stored in the Java heap The object operates as a reference to this memory. This can significantly improve performance in some scenarios.

Guess you like

Origin blog.csdn.net/weixin_43663421/article/details/109124745