Method execution in the stack frame

Execution of method in stack frame (jdk8)

1. Virtual machine stack components The
main components of jvm virtual machine (HotSpot): program counter, method area, virtual machine stack, local method stack, and heap.
Among them, the program counter, the virtual machine stack, and the local method stack are private to the thread. They are generated with the creation of the thread, and disappear with the death of the thread.
The method area and the heap are areas shared by threads.
2. What are the components of the stack?
The components of the stack are individual stack frames. The method calls in java correspond to the stacking and popping operations of the stack frame (one method call will correspond to the stacking once). The stack is thread-private and the read and write speed is faster. , But the memory is small and the default size is 1024kb. Because it is private to the thread, each thread can set its own stack space size. You can use -Xss1M, and the value in the stack is shared. Generally speaking, the basic data types are stored in The stack is actually stored in the stack frame. The stack frames are independent of each other. The data in a stack frame can be shared. For example,

int a = 1;
int b = 1;

In the above code, there is actually only one 1 in a stack frame, and the two variables are actually a value referenced. This is the data sharing of the stack. (To create a basic type variable, the stack first goes to the space to find the value, reference, and no creation)
3. What are the components of the stack frame?
The stack frame includes: local variable table (local variables in the storage method), operand stack (storage of intermediate variables, data calculation needs to be pushed into the stack first), dynamic link, and return address.

Give a program chestnut to explain the execution process of the method in the stack frame:

public void test1(){
    
    
		int i = 6;
		int j = 9;
		int h = i + j;
	}

Look at the class file information corresponding to this program, and use the javap -verbose Test.class command to view the class file information.
Insert picture description here
As can be seen from the above figure, the variables of the method will enter the local variable table after passing through the operand stack. When operating on local variables, the data in the local variable table must be pushed into the stack before the calculation can be performed, and the data can be referenced. In fact, the type is the same. It goes through the operand stack before entering the local variable table. This is a simple process of a method in a stack frame.
If anything is wrong, please testify, thank you.

Guess you like

Origin blog.csdn.net/m0_46897923/article/details/109676991