Summary of knowledge points of stack running in java virtual machine

  • 2021-11-13 08:20:13
  • OfStack

Operating principle

1. Stack frames contained in different threads are not allowed to refer to each other.

2. 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 pin, and the virtual machine will discard the current stack frame, so that the previous stack frame will become the current stack frame again.

3. Java method has two ways to return functions, one is normal function return, using return instruction; The other is to throw an exception. Either way, the stack frame will be ejected.

Instances


public class StackFrameTest {
    public static void main(String[] args) {
        StackFrameTest stackFrameTest = new StackFrameTest();
        stackFrameTest.method1();
    }
 
    public void method1(){
        System.out.println("method1() Start execution ");
        method2();
        System.out.println("method1() End of execution ");
    }
    public int method2(){
        System.out.println("method2() Start execution ");
        int i = 100;
        int m = (int)method3();
        System.out.println("method2() Coming to an end ");
        return i + m;
    }
    public double method3(){
        System.out.println("method3() Start execution ");
        double j = 3.1;
        System.out.println("method3() Coming to an end ");
        return j;
    }
}

Expansion of knowledge points:

1. Java stack is also called virtual machine stack, which is what we often call stack. The Java stack is the memory model for Java method execution.

2. The Java stack contains stack frames, each of which corresponds to a called method. The stack frame includes a table of local variables (Local Variables), an operand stack (Operand Stack), a reference to the running time pool of the class to which the current method belongs (the concept of running time pool will be discussed in the method section) (Reference to runtime ES29pool), a method return address (Return Address) and some additional information. When a thread executes a method, a corresponding stack frame is created and the established stack frame is pressed onto the stack. When the method is finished, the stack frame will be taken out of the stack. Therefore, there is no recycle resource 1 theory for the method.

3. Local variable table, as its name implies, presumably needs no explanation. Everyone should understand its function. Is used to store local variables in the method (including non-static variables declared in the method and function parameters). For variables of basic data type, the value is stored directly, and for variables of reference type, the reference to the object is stored. The size of the local variable table can be determined by the compiler, so the size of the local variable table will not change during the execution of the program.

4. Operand stack. Friends who have learned the stack in data structure must be familiar with expression evaluation. The most typical application of stack is to evaluate expressions. Think about the process of executing a method by a thread, which is actually the process of continuously executing statements, but in the final analysis, it is the process of computing. Therefore, it can be said that all the calculation processes in the program are based on operands.

The above is the java virtual machine stack running knowledge summary of the details, more on the java virtual machine stack running principle of information please pay attention to other related articles on this site!


Related articles: