Virtual machine stack
Due to the cross-platform design, Java instructions are designed according to the stack. Different platforms have different CPU architectures, so they cannot be designed as register-based.
The advantage is that it is cross-platform, the instruction set is small, and the compilation is easy to implement;
The disadvantage is that the performance is reduced, and more instructions are needed to achieve the same function.
What is a virtual machine stack
The stack is the runtime unit (how to process data), and the heap is the unit for storing data (how to put the data and where to put it).
When each thread is created, a virtual machine stack is created, which saves a stack of frames inside, corresponding to this time of Java method calls.
There is a one-to-one correspondence between stack frames and methods.
Life cycle: the same as threads.
Role: in charge of the operation of the Java program, it saves the local variables of the method (8 data types, the reference address of the object), partial results, and participates in the call and return of the method.
Advantages of the stack
The stack is a fast and effective way of allocating storage, the access speed is second only to the program counter.
The JVM directly operates on the Java stack in two ways:
(1) Each method is executed, accompanied by push (push, push)
(2) Stack operation after execution
There is no garbage collection problem for the stack.
Possible exceptions in the stack
Fixed-size Java virtual machine:
If the stack capacity allocated by the thread request exceeds the maximum capacity allowed by the Java virtual machine stack, the Java virtual machine will throw a StackOverflowError exception.
Dynamically scalable Java virtual machine:
When enough memory is not available, or there is no memory to create a new Java virtual machine, OutOfMemoryError will be thrown.
Stack storage unit
Each thread has its own stack, and the data in the stack exists in the format of a stack frame.
Each method being executed on this thread corresponds to a stack frame.
The stack frame is a memory block and a data set that maintains various data sets during the execution of the method.
How the stack works
Enter the method to push the stack , and the method ends to pop out of the stack .
In an active thread, there will only be one active stack frame at a point in time.
Current stack frame: The stack frame of the method currently being executed.
Current method: the method corresponding to the current stack frame.
Current class: The class that defines this method.
All bytecode instructions run by the execution engine only operate on the current stack frame.
If other methods are called in this method, the corresponding new stack frame will be created and placed at the top of the stack, becoming the new current frame.
The stack frames contained in different threads are not allowed to interact, that is, it is impossible to introduce the stack frame of another thread into one stack frame.
If the current method calls other methods, when the method returns, the current stack frame will return the execution result of this method to the current stack frame. Then, the virtual machine discards the current stack frame so that the previous stack frame becomes the current stack frame again.
Java has two ways to return functions:
- To return from a normal function, use the return instruction.
- Throw an exception.
The internal structure of the stack frame
Local variable table
Method return address
Some additional information
Local variable table (important)
Classification of variables
- Basic data types, reference data types
- Member variables (all are initialized by default before use): class variables (default assignments to class variables in the prepar phase of linking, and assignments to class variables and static code block assignments in the initial phase), instance variables (as the object is created, The instance variable space will be allocated in the pair space, and the default assignment will be performed)
- Local variables (display assignment must be performed before use, otherwise the compilation will not pass)
The local variable table is defined as an array, which is mainly used to store method parameters (formal parameters) and local variables defined in the method body. The most basic storage unit is Slot.
The local variable table can store various basic data types known to the compiler, 8 data types, reference types (referance), and returnAddress type variables.
Types within 32 bits occupies one Slot, including returnAddress type, 64-bit types (long and double) occupies two Slots, byte, short, char are converted to int before storage, boolean is converted to int before storage, 0 is false, non-zero is true, the local variable table is built on a thread-private stack, which is thread-safe.
The JVM will assign an access index to each Solt in the local variable table, through which the local variable values in the local variable table can be accessed.
Double and long occupy two Solts and only need one index.
If the current frame is created by a construction method or an instance method, then the object reference this will exist in the Slot with index 0. Non-static methods will create a parameter of this with index 0, and the remaining parameters are arranged in order The static method is not allowed to use this because the index of this is not put in the static method.
In addition to the local variable table, each independent stack frame also contains a first-in-last-out operand stack. In the process of method execution, it writes data to or extracts data from the stack according to bytecode instructions, that is, pushes or pops the stack.
The operand stack is mainly used to save the intermediate results of the calculation process, and at the same time as a temporary storage space for variables in the calculation process.
The operand stack is a work area of the JVM execution engine. When a method is first executed, a new stack frame is also created at the same time. The operand stack of this method is empty.
When each operand stack is created, its depth is determined to store the value, and it is stored in the Code attribute of the method as the max_stack value.
The storage words of the operand stack and the local variable table are the same, with 32 bits occupying one depth and 64 bits occupying two.
The operand stack does not perform data access based on the index, but can only complete a data access through standard stacking and popping operations.
If the called method has a return value, its return value will be pushed into the operand stack of the current stack frame, and the next bytecode instruction to be executed in the PC register is updated.
Dynamic linking: Each stack frame saves a runtime constant pool that can point to the class of the current method. The purpose is: If you need to call other methods in the current method, you can find the corresponding symbol reference from the runtime constant pool. Then the symbol reference is converted to a direct reference, and then the corresponding method can be called directly, which is dynamic linking.
Method return address
Store the value of the PC register that calls the method
There are two ways to end a method
End of normal execution
An unhandled exception occurred, abnormal exit