Introduction to the JVM virtual machine stack

1. The structure of the virtual machine stack

​ The Java virtual machine stack was called the java stack in the early days. Every time a thread is created, a virtual machine stack is created correspondingly. Its life cycle is consistent with the thread life cycle. The virtual machine stack is composed of stack frames, and each stack frame corresponds to a method. A method call corresponds to a stack frame being pushed into the stack, and a method ends and a stack frame is popped out of the stack. Each stack frame in the stack consists of local variable table, operand stack, dynamic link, method return address, and additional information. The structure of the stack is as follows:

insert image description here

2. The characteristics of the virtual machine stack

  1. The virtual machine stack is a fast and effective way to allocate storage, and its access speed is second only to the program counter

  2. The virtual machine stack has only two operations: push and pop

    The method call is pushed into the stack, and the method is popped out when the method finishes running.

  3. There is no garbage collection problem in the virtual machine stack

  4. The virtual machine stack may have the following two exceptions: StackOverFlowError, OutOfMemoryError

    The size of the virtual machine stack can be set dynamically or fixed. When the stack size is fixed, if the sum of all stack frame sizes is greater than the stack size, or a stack frame is larger than the stack, a StackOverFlowError exception will be reported; when the stack size is dynamic, the stack cannot apply for a larger stack space , or the memory size cannot create a new stack, an OutOfMemoryError exception will be reported.

3. The operating principle of the virtual machine stack

  • In an active thread, only one stack frame is allowed to be active at a point in time. Only the stack frame corresponding to the currently executing method is valid, and the currently valid stack frame becomes the current stack frame (stack top stack frame), called the current stack frame, the corresponding method is called the current method, and the class that defines this method is called the current stack frame. kind.
  • If the current method calls other methods, a new stack frame is created and pushed onto the stack, becoming the new current stack frame.
  • The bytecode instructions executed by the execution engine are only for the current stack frame.
  • A stack frame of another thread cannot be referenced in a stack frame of another thread.
  • The method has two return methods: one is to return normally, and the other is to throw an exception, both of which will cause the stack frame to pop out of the stack.

The following code describes method invocation and stack frame activity:

/**
 * @author cx
 * @date 2022/10/24
 */
public class StackDemo01 {
    
    

    public static void main(String[] args) {
    
    
        System.out.println("main方法开始运行...");
        method1("main");
        System.out.println("main方法运行结束...");
    }

    static void method1(String name){
    
    
        System.out.println("method1被" +name+ "方法调用了..." );
        method2("method1");
        System.out.println("method1运行结束...");
    }

    static void method2(String name){
    
    
        System.out.println("method2被" +name+ "方法调用了..." );
        method3("method2");
        System.out.println("method2运行结束...");
    }

    static void method3(String name){
    
    
        System.out.println("method3被" +name+ "方法调用了..." );
        System.out.println("method3运行结束...");
    }

//    console:
//    main方法开始运行...
//    method1被main方法调用了...
//    method2被method1方法调用了...
//    method3被method2方法调用了...
//    method3运行结束...
//    method2运行结束...
//    method1运行结束...
//    main方法运行结束...
}

insert image description here

Guess you like

Origin blog.csdn.net/chenxingxingxing/article/details/127505416
Recommended