The data areas in the JVM are grouped into virtual machine stacks, Java heaps, method areas, run-time pools, program counters (PC registers). In these five classes, the virtual machine stack is used to represent the area of method execution in each thread, and the stack frame is the stack element in the virtual machine stack. Stack frames are data structures that support virtual machines for method invocation and method execution. Each method from the start of the call to the completion of the process can be seen as a stack frame in the virtual machine stack from the stack to the process.
There are many stack frames in one thread, only the stack frame at the top of the stack is valid, the stack frame at the top of the stack is called the current stack frame , and the current stack frame association method is called the current method . The byte code instruction of the virtual machine's execution engine only works on the current stack frame (current method).
stack frame composition local variable table operand stack dynamic link return address other information
local Variable Table
The local variable table, as the name suggests, is used to store local variables in the method. It is worth noting that it is not just stored in the local variables declared within the method, the variables in the method argument list , the This pointer implied by the non-static method, The exception object variables in the catch in the Try-catch block are variables in the local variable table. The size of a local variable table is determined before it is run because a property in the code property of the method table of the compiled class file of the Java file is used to represent the local variable table size required by the method.
The capacity of a local variable table is in the variable slot (Slot), and the virtual machine specification does not specify how big a variable slot is, just a very directional representation. Data except long and double are stored in a Slot size, Long and double are stored with two slot. It is also important to note that the size of the local variable table is not a simple addition of each variable size, but by calculating the scope of each variable, the scientific calculation of a minimum size. For example, a local variable that is invalidated after a paragraph, leaving room for other variables to do space without requiring more space for the local variable table.
operand Stacks
The operand stack is also called the Operation Stack, which is a LIFO stack, which is used to perform some operations in the method. Similar to the local variables table, the Java file compiled in the class file has the maximum depth of the number of operands stack. Many books use Iadd to give examples, this is a simple example of a Iadd byte code instruction that adds two int types of data, so before this bytecode executes, we must have pressed the int type data in the two local variables table into the operand stack through some bytecode. Then the operation is done by adding iadd. It is important to note that in the operand stack, only long and double can occupy two depths.
In fact, operand stacks are very important, and the Java Virtual machine's interpretation execution engine is called a "stack based execution engine", where the "stack" is the operand stack. And through some optimization techniques, one operand stack of two stacks and another local variable table overlap, so that some additional parameter transfer overhead can be omitted when the method is called.
Dynamic Link
Each stack frame contains a reference to the Run-time pool of the type in which the current method resides so that dynamic linking is implemented for the code of the current method. In a class file, if a method calls another method, or accesses its member variables, it needs to be represented by a symbolic reference, then the function of the dynamic link is to resolve the method or variable represented by the symbolic reference to a direct reference at the appropriate time.
return address
We say that the stack frame includes the return address, which may be said somewhat reluctantly, after all, the return address is a return of the state and concept, not a specific structure. When a method starts executing, there are only two ways to exit this method. The first approach is when a return statement is encountered while the method is executing (an instruction that returns a byte code is encountered), which exits the method normally and passes the return value of the corresponding method to the caller of the method. The second approach is when an exception is encountered in the method, and the exception is not captured by Try-catch (when discussing the internal structure of a virtual machine I say that try-catch this Java syntax is not very good, But in order to be able to express clearly that I did so, or that I throw throw an exception (Athrow instruction throws an exception). In both cases, the method exits as long as the exception handler is not matched in this method, and an exception exit method is not given a return value to the caller of the method.
The normal exit condition, which is returned to the caller of the method where it is called (the specific location is recorded by the PC Register). And the exception of the exit return address to be determined through the exception handler, the stack frame generally do not save this part of the information.
Other Information
The virtual machine specification allows specific virtual machines to add information that is not in the specification when implemented. In the process of actual development, we usually classify the dynamic link , method return address and other additional information as a class.