A Stack Frame is a data structure used to support vm method invocation and method execution. It is a Stack element of the Virtual Machine Stack in the data area when the VM runs. The stack frame stores information about a method’s local variator, operand stack, dynamic linkage, and method return address. The first method corresponds to the process of a stack frame in the virtual machine stack from the beginning of the call to the completion of execution.
Each stack frame contains a local variable table, operand stack, dynamic linkage, method return address, and some additional information. At the time of the compiled Code, how much local variables in the stack frame, how deep the operand stack are completely determined, and write Code attributes the method table, so a stack frame needs to how much memory allocation, not affected by the program run time variable data, but only depends on the specific implementation of the virtual machine.
The chain of method calls in a thread can be long, with many methods handling execution state simultaneously. For the execution engine, only the Stack Frame at the top of the virtual machine Stack is valid in the active thread, called the Current Stack Frame, and the Method associated with this Stack Frame is called the Current Method. All bytecode instructions run by execution references operate only on the current stack frame. The conceptual structure of stack frame is shown in the figure below:
1. Local variation scale
Local Variable Table is a set of Variable value storage space used to store method parameters and Local variables defined within a method. When the Class file is compiled, the maximum size of the local variable table that the method needs to allocate is determined in the max_locals data item of the method’s Code property.
A Variable Slot is the smallest unit of a local Variable table. It is not mandatory to have 32 bits, although 32 bits are sufficient to hold most types of data. A Slot can hold Boolean, byte, CHAR, short, int, float, Reference, and returnAddress. Reference refers to a reference to an object instance, which can be used to obtain the index of the starting address of the object stored in the Java heap and the type information of the data type in the method area. ReturnAddress refers to the address of a bytecode instruction. For 64-bit long and double variables, the virtual machine allocates two consecutive Slot Spaces.
A VM uses a local change table based on index location. As we know, the ** local variable table stores method parameters and local variables. When calling a method that is non-static, the Slot in the zeroth index of the local variable table defaults to passing a reference to the instance of the object to which the method belongs, that is, the object to which the “this” keyword points. After the method parameters are allocated, local variables defined within the method are allocated in turn.
Slot multiplexing verification
To save stack frame space, slots in the local variable table can be reused. When some variables are out of scope, the Slot corresponding to those variables can be given to other variables. This mechanism can sometimes affect garbage collection behavior.
public class Main { public static void main(String[] args) { byte[] placeholder = new byte[64*1024*1024]; System.gc(); }}Copy the code
[GC (system.gc ()) 69468K->66384K(188416K), 0.0016481 secs] [Full GC (system.gc ()) 66384K->66280K(188416K), 0.0079337 secs]Copy the code
public class Main { public static void main(String[] args) { { byte[] placeholder = new byte[64*1024*1024]; } int a = 0; System.gc(); }}Copy the code
[GC (system.gc ()) 69468K->66368K, 0.0012876 secs] [Full GC (system.gc ()) 66368K->744K(188416K), 0.0055897 secs]Copy the code
As you can see, when I put byte’s declaration in a separate code block and then execute code out of scope, the GC collects slot.
Note: The JVM does not assign initial values to local variables, only to global variables.
2. Operand stack
An Operand Stack, also known as an operation Stack, is a last-in, first-out Stack. Max_stacks in the Code attribute of the Class file specifies the maximum stack depth during execution. The interpretation execution engine of the Java virtual machine is called “stack-based execution engine”, where the stack refers to the operand stack.
The operand stack is used to perform arithmetic operations during method execution or to call other methods to pass arguments.
Optimization of operand stacks by the JVM
In the conceptual model, the two stack frames are independent of each other. But most virtual machine implementations are optimized so that the two stack frames overlap. Overlap the bottom partial operand stack with the top local variable table so that a portion of the data can be shared during method calls without additional parameter copying.
3. Dynamic linking
Each stack frame contains a reference to the method that the stack frame belongs to in the execution runtime constant pool. This reference is held to support Dynamic Linking during method invocation.
The Class file holds a large number of symbolic references, and the method invocation instructions in bytecode take symbolic references to methods in the constant pool as arguments. Some of these symbolic references are converted to direct references during class loading or the first time they are used, which is called static resolution. The other part is converted to a direct reference during each run, called the dynamic join.
4. The method returns the address
When a method starts executing, there are only two ways to exit the current method:
- When a return command is encountered, the return value is passed to the upper-level Method caller. This exit is called Normal Method Invocation Completion.
The caller's PC counter can be used as the return address
. - When an exception is encountered and there is no processing in the Method body, the Method exits without returning a value, called the Abnormal Method Invocation Completion.
The return address is determined by the exception handler table
.
When the method returns, there are three possible operations:
restore
The local variable table and operand stack of the upper method- The return value
Push the
Caller The operand stack of the caller stack frame Adjust the
The value of the PC counter points to an instruction following the method call instruction
5. Additional information
The virtual machine specification allows a specific virtual machine implementation to add information to the stack frame that is not described in the specification, such as debugging information, depending on the specific virtual machine implementation. In practical development, dynamic connection, method return address and other additional information are generally grouped together as stack frame information.