JVM---stack frame

Stack frame in the Java virtual machine stack

Insert picture description here

The above figure describes the situation of a certain Java virtual machine stack. There may be multiple stack frames in a certain Java virtual machine stack. Stack frame (Frame) is a data structure used to store data and partial process results , and it is also used to handle dynamic linking, method return values, and exception dispatch.
The stack frame is created as the method is called, and destroyed as the method ends. Each stack frame is a record of the running status of the called method. The storage space of the stack frame is allocated in the Java virtual key stack by the thread that created it. The basic components of each stack frame are:

 - 本地变量表(局部变量表)
 - 操作数栈
 - 指向当前方法所属类的运行时常量池的引用
 - 其他信息

During the running of a thread, there may be multiple stack frames. Only the stack frame of the method currently being executed is active. This stack frame is called the current stack frame. The method corresponding to this stack frame is called the current method. Definition The class of this method is called the current class. Various operations on the local variable table and operand stack are directed to the local variable table and operand stack of the current stack frame. The local variable table and operand stack are provided to the Java virtual machine for use, that is, using Java The virtual machine instructions to manipulate the local variable table and operand stack.

1. Local variable table

As the name implies, the local variable table is a table used to store the local variables in the current method, so the size of the local variable table can be determined at compile time. What exactly is that local variable?
A local variable can store data of type boolean, byte, char, short, int, float, reference, and two consecutive local variables can store data of type long or double. Obviously, the space occupied by a local variable It is 4 bytes. For data less than 4 bytes, it will be automatically promoted to int type.
Insert picture description here
As shown in the figure above, the local scalars of the method are arranged in the local variable table in this order (the data byte order in Java is big-endian, and the 64-bit data in the Java virtual machine does not need to be aligned). The local variable table is like an array, so when accessing a local variable, we can use the subscript of the variable to access. The index value of the first local variable is 0, and the index of the data occupying two local variables adopts the occupied The smaller index value of the two local variables.
The Java virtual machine uses the local variable table to complete the parameter transfer when the method is called. There is a little difference between the local variable table for calling class methods and instance methods. When a class method is called, the parameters of the method will be sequentially passed to the continuous position starting from 0 in the local variable table; when the instance method is called, the 0th local variable must be the reference of the object where the instance method is stored (that is, we Commonly used this pointer), other subsequent parameters will be passed to the continuous position starting from 1 in the local variable table .

2. Operand stack

The operand stack has the characteristics of a stack, first in and last out. The maximum depth of the operand stack in the stack frame is determined by the compiler.
When the stack frame is just created, the operand stack is an empty stack. The Java virtual machine provides bytecode instructions to copy constants or variables from the fields of the local variable table or object instance to the operand stack. It also provides instructions to take data from the operand stack, manipulate data, and manipulate The result is re-stacked. When calling a method, the operand stack is also used to prepare the parameters of the calling method and receive the result returned by the method.
For example, there is this piece of code:

int i ;
i++;

The actual bytecode instruction looks like this:

iconst_0		//	将常量0压入操作数栈
istore_1		//  将栈中的操作数弹出,放在索引为1的局部变量中,即将0赋给i
iload_1		    //  将变量1压入到操作数栈中
iconst_1		//  将常量1压入操作数栈
iadd 			//	从操作数栈中弹出两个数,相加后入栈
istore_1		//	从栈中弹出一个数并放在i中

As can be seen from the above piece of code, after the code we wrote in java is translated into bytecode by the java compiler, the variables we declare and define in the method are stored in the local variable table. When operating on the variables, we need Frequent pops and pushes.

3. Reference to the runtime constant pool

Each stack frame contains a reference to the runtime constant pool of the class where the current method is located, so as to dynamically link the code of the current method. In the class file, if a method needs to call other methods or access member variables, the method represented by the symbolic reference needs to be converted into a direct reference to the actual method.

Guess you like

Origin blog.csdn.net/Miha_Singh/article/details/88722689