Virtual machine stack-01

Virtual machine stack-01

1. Overview of the virtual machine stack

The background of the virtual machine stack

Due to the cross-platform design, Java instructions are designed according to the stack. Different platforms have different CPU architectures, so they cannot be designed as register-based.

The advantage is that it is cross-platform, the instruction set is small, and the compiler is easy to implement. The disadvantage is that the performance is reduced, and more instructions are required to achieve the same function.

Stack and heap in memory

The stack is the unit of runtime, and the heap is the unit of storage.

That is: the stack solves the running problem of the program, that is, how the program is executed, or how to process the data. The heap solves the problem of data storage, that is, how and where to put the data.

Insert picture description here

Basic content of virtual machine stack

  • What is the
    Java virtual machine stack ? Java Virtual Machine stack (Java Virtual Machine stack), also called Java stack in the early days.

    When each thread is created, a virtual machine stack is created, and a stack frame is stored inside, corresponding to Java method calls.

    • Is thread private
  • Life cycle The
    life cycle is the same as the thread.

  • effect

    Responsible for the operation of the Java program, it saves the local variables of the method (8 basic data types, the reference address of the object), partial results, and participates in the call and return of the method.

    • Local variables vs member variables (or attributes)
    • Basic data variable vs reference type variable (class, array, interface)

Advantages of the stack:

  • The stack is a fast and effective way of allocating storage, the access speed is second only to the program counter .

  • The JVM has only two direct operations on the Java stack:

    • Each method is executed, accompanied by push to stack (push into stack, push into stack)

    • Stack work after execution

  • There is no garbage collection problem for the stack

  • There is OOM, there is no GC

Interview question: What are the exceptions encountered in development?

Possible exceptions in the stack

  • The Java virtual machine specification allows the size of the Java stack to be dynamic or fixed .
    • If a fixed-size Java virtual machine stack is used, the Java virtual machine stack capacity of each thread can be independently selected when the thread is created. If the stack capacity allocated by the thread request exceeds the maximum capacity allowed by the Java virtual machine stack, the Java virtual machine will throw a stackOverflowError exception .
    • If the Java virtual machine stack can be dynamically expanded, and cannot apply for enough memory when trying to expand, or there is not enough memory to create the corresponding virtual machine stack when creating a new thread, the Java virtual machine will throw a OutOfMemoryError is abnormal .

Set the stack memory size

We can use the parameter -Xss option to set the maximum stack space of the thread. The size of the stack directly determines the maximum reachable depth of the function call.

Test code:

package com.lbl.stack;

public class stackXssTest {
    
    
    static int count =0;
    public static void main(String[] args) {
    
    
        System.out.println(count);
        count++;
        main(args);
    }
}

Insert picture description here

Now I modify the stack memory size

Insert picture description here

Add to:-Xss256k
Insert picture description here

Run the above code again:

Insert picture description here

2. Stack storage unit

What is stored in the stack?

  • Each thread has its own stack, and the data in the stack is in the format of a stack frame.
  • Each method being executed on this thread corresponds to a stack frame (stack Frame)
  • The stack frame is a memory block and a data set that maintains various data information during the execution of the method.

How the stack works

  • JVM Java stack, the direct operation of only two, is on the stack frame of push and pop , follow the "last out" / "last in, first out" principle .
  • In an active thread, there will only be one active stack frame at a point in time. That is, only the stack frame (top stack frame) of the currently executing method is valid. This stack frame is called the current stack frame (current frame) , and the method corresponding to the current stack frame is the current method (CurrentMethod) , the definition class of this method is that when the former category (Current class) .
  • All bytecode instructions run by the execution engine only operate on the current stack frame.
  • If other methods are called in this method, a corresponding new stack frame will be created and placed on the top of the stack as the new current frame.

Insert picture description here

  • The stack frames contained in different threads are not allowed to reference each other, that is, it is impossible to reference the stack frame of another thread in one stack frame.
  • 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 previous stack frame, and then the virtual machine discards the current stack frame, making the previous stack frame the current stack frame again
  • There are two ways to return a function in a Java method, one is to return from a normal function, using the return instruction; the other is to throw an exception. No matter which method is used, the stack frame will be popped.

The test code is as follows:

package com.lbl.stack;

public class StackFrameTest {
    
    
    public static void main(String[] args) {
    
    
        method1();
    }

    private static void method1() {
    
    
        System.out.println("method1..开始执行!");
        method2();
        System.out.println("method1..即将结束!");
    }

    private static void method2() {
    
    
        System.out.println("method2..开始执行!");
        method3();
        System.out.println("method2..即将结束!");
    }
    private static void method3() {
    
    
        System.out.println("method3..开始执行!");
        System.out.println("method3..即将结束!");
    }

}

The running effect is as follows:

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_37924905/article/details/108681162
Recommended