Structure of Java Virtual Machine 3

1 Run-Time Data Areas

1.1 The pc Register

1.2 Java Virtual Machine Stacks

1.3 Heap

1.4 Method Area

The JVM method area is a memory area shared by all threads of the JVM. In programming languages, the method area is used to store compiled code. In the operating system process, the method area is used to store text segments. In the JVM, the method area is used to store The structure of each class, including the runtime constant pool, scope data, method data, method code, constructor code, class and interface initialization methods, and instance initialization methods.

The JVM method area is created when the JVM starts. It is a logical part of the JVM heap area. It supports setting a fixed size, expanding or compressing according to the calculation needs at runtime. Since the JVM method area is a logical memory space, A contiguous physical space is not necessarily required. An exception will occur in the JVM method area under the following conditions:

  • If the memory space required for the application or expansion of the JVM method area is insufficient, the JVM throws an exception of OutOfMemoryError

1.5 Run-Time Constant Pool

The JMV runtime constant pool is a representation of the constant_pool constant table in the class class file corresponding to each class or interface, which includes various types of constants, including literal numbers generated at compile time and reference constants that can only be processed at runtime , the JVM constant pool is similar to the symbol table of common programming languages, but it covers a wider range of data. Each runtime constant pool is applied in the JVM method area, and the JVM constructs its corresponding constant pool when each class or interface is created. When the JVM builds the constant pool of each class or interface, an exception will occur under the following conditions:

  • If the memory space required for the JVM constant pool application or expansion is insufficient, the JVM throws an OutOfMemoryError exception

1.6 Native Method Stacks

The implementation of the JVM uses a common stack similar to the C language, and supports native local methods (methods implemented in non-Java language, for example, methods implemented in C language), and the native local method stack is constructed when each thread is created.

The native local method stack of the JVM supports fixed size or dynamic expansion. The stack space can be calculated and compressed as needed. If the stack space is a fixed size, the address space needs to be selected separately when the stack is created. In terms of implementation, the JVM can provide programmers or users with control over the initialization size of the JVM stack, dynamic expansion and compression, and setting the maximum and minimum values ​​of the JVM stack. The JVM stack will be abnormal under the following conditions:

  • If the stack space required by a thread during calculation is greater than the value allowed by the JVM stack, the JVM throws a StackOverflowError exception

  • If the JVM stack can be dynamically expanded, the required memory space is insufficient when the stack is expanded, and the required memory space is insufficient when creating a thread, then the JVM throws an OutOfMemoryError exception

2 Frames

Each box is used to store data and partial results, perform dynamic linking, return results for methods, and distribute exceptions. Each new box is created each time a method is called. When the method call is completed, the box Destroyed, frames are created by threads applying from the JVM stack, and each frame includes an array of local variables corresponding to the method, an operand stack of the method, and a reference to the runtime constant pool of the class corresponding to the method.

The size of the local variable array and the operand stack are determined at compile time and follow the method code corresponding to the frame frame, so the memory space for these structures can be created when the method is called.

During the execution of a thread, only one frame corresponding to the executing method can be active at the same time point. This frame is called the current frame, and the method corresponding to it is called the current frame. method, the class corresponding to the current method is called the current class, and operations on local variables or operand stacks generally refer to the current frame.

During the execution of a thread, when a current method is called or a current method calls another method, the current method is no longer current. When a new method is called, a new frame is created and becomes the current method, and thread control passes into the new current method. The current method returns, and the current frame box also returns the call result. When the previous frame box is reached, the previous frame box will become the current frame box. Frames created by a thread are local and cannot be referenced by other frames.

2.1 Local Variables

(to be continued)

おすすめ

転載: blog.csdn.net/uesowys/article/details/129635739