Virtual Machine Stack Overview

I. Overview

1.1, virtual machine stack background

        Due to the cross-platform design, Java instructions are designed based on the stack. The CPU architecture of different platforms is different, so it 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.

1.2, virtual machine stack content

1.2.1, the concept of virtual machine stack

        Java Virtual Machine Stack (Java Virtual Machine Stack), also known as the Java stack in the early days. Each thread will create a virtual machine stack when it is created, and each stack frame (Stack Frame) is stored inside. Each stack frame corresponds to a Java method call and is private to the thread.

1.2.2, life cycle

        The life cycle of the Java virtual machine stack is consistent with the thread

1.2.3. Function

        In charge of the running of the Java program, save the local variables and partial results of the method, and participate in the calling and returning of the method.

1.2.4, the characteristics of the stack

        The stack is a fast and efficient way to allocate storage, and its access speed is second only to the program counter. There are only two direct operations of the JVM on the Java stack:

  • Each method is executed, accompanied by push (push, push)
  • Popping work after execution

        There is no garbage collection problem for the stack (the stack overflows)

1.3. Exceptions in stack development

1.3.1. 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 requested by the thread exceeds the maximum capacity allowed by the Java virtual machine stack, the Java virtual machine will throw a StackOverflowError exception. For example, if the program continues to make recursive calls, and there is no exit condition, it will cause the stack to be pushed continuously.
  • If the Java virtual machine stack can be dynamically expanded, and cannot apply for enough memory when trying to expand, or if 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 exception.

Two, the storage unit of the stack

2.1, the content stored in the stack

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

2.2, stack operation principle

2.2.1. Operation process

  • There are only two direct operations of the JVM on the Java stack, which is to push and pop the stack frame, following the "first in, first 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 (stack 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 (Current Method). The class that defines this method is the current class (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, the corresponding new stack frame will be created and placed on the top of the stack to become the new current frame.

2.2.2. Note

  • The stack frames contained in different threads are not allowed to have mutual references, that is, it is impossible to refer to the stack frame of another thread in a 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 become the current stack frame again.
  • There are two ways to return a function in a Java method, one is a normal function return, using the return instruction; the other is to throw an exception. Either way, the stack frame will be popped.

2.3, the internal structure of the stack frame

  • Local Variables Table (Local Variables)
  • Operand Stack (or Expression Stack)
  • Dynamic Linking (or a method reference to the runtime constant pool)
  • Method return address (Return Address) (or the definition of method exit normally or abnormally)
  • some additional information

2.3.1. Local variable table (local variable table)

  • Defined as a numeric array, it is mainly used to store method parameters and local variables defined in the method body. These data types include various basic data types, object references (reference), and returnAddress types.
  • Since the local variable table is built on the thread's stack and is the private data of the thread , there is no data security problem
  • The required capacity of the local variable table is determined at compile time and stored in the maximum local variables data item of the Code attribute of the method. The size of the local variable table is not changed during the execution of the method.
  • The number of method nested calls is determined by the size of the stack. Generally speaking, the larger the stack, the more nested method calls. For a function, the more parameters and local variables it has, the larger the local variable table will be, and the larger its stack frame will be, so as to meet the increasing demand for the information passed by the method call. In turn, function calls will take up more stack space, resulting in a reduction in the number of nested calls.
  • The variables in the local variable table are only valid in the current method call . When the method is executed, the virtual machine completes the transfer process of the parameter value to the parameter variable list by using the local variable table. When the method call ends, as the method stack frame is destroyed, the local variable table will also be destroyed.

1. slot variable slot

  • Local variable table, the most basic storage unit is Slot (variable slot)
  • The storage of the parameter value always starts at index0 of the local variable array and ends at the index of the array length -1.
  • Various basic data types (8 types), reference type (reference), and returnAddress type variables known at compile time are stored in the local variable table.
  • In the local variable table, types within 32 bits only occupy one slot (including the returnAddress type), and types of 64 bits (long and double) occupy two slots.
    • byte, short, and char are converted to int before storage, boolean is also converted to int, 0 means false, non-zero means true.
    • long and double occupy two Slots
  • The JVM will assign an access index to each Slot in the local variable table, through which the local variable value specified in the local variable table can be successfully accessed
  • When an instance method is called, its method parameters and local variables defined inside the method body will be copied to each slot in the local variable table in order
  • If you need to access a 64bit local variable value in the local variable table, you only need to use the previous index . (For example: access to long or double type variables)
  • If the current frame is created by a construction method or an instance method, then the object reference this will be stored in the slot with index 0, and the rest of the parameters will continue to be arranged in the order of the parameter list.

2. Slot reuse

        The slots in the local variable table in the stack frame can be reused. If a local variable has passed its scope, the new local variable declared after its scope is likely to reuse the slot of the expired local variable. bit, so as to achieve the purpose of saving resources.

3. Comparison of static variables and local variables

Variable classification:

  • According to data type: basic data type, reference data type
  • According to the position declared in the class:
    • Member variables ( static modified static variables , instance variables): have undergone default initialization assignment before use
      • Class variables: In the prepare phase of linking, assign values ​​to class variables by default; in the initial phase, assign values ​​to class variables explicitly, that is, assign values ​​to static code blocks
      • Instance variable: With the creation of the object, the instance variable space is allocated in the heap space and the default value is assigned
    • Local variables: must be explicitly assigned before use, otherwise, the compilation will not pass

 Note: Variables in the local variable table are important root nodes for garbage collection. Objects that are directly or indirectly referenced in the local variable table will not be collected.

2.3.2, operand stack

        In addition to the local variable table, each independent stack frame also contains the operand stack (expression stack)

  • Operand stack, in the process of method execution, according to the bytecode instruction, write data to the stack or extract data, that is, push (push) and pop (pop)
  • The operand stack is mainly used to save the intermediate results of the calculation process, and at the same time as a temporary storage space for variables during the calculation process.
  • Any element in the stack can be any Java data type
    • The 32bit type occupies a stack unit depth
    • The 64bit type occupies two stack unit depths
  • The operand stack can only access data by pushing in and out of the stack, and cannot use indexes

top-of-stack caching

       Cache the top elements of the stack in the registers of the physical CPU, reduce the number of reads and writes to the memory, and improve the execution efficiency of the execution engine.

2.3.3, dynamic link

concept:

        Each stack frame internally contains a reference to the method to which the stack frame belongs in the runtime constant pool . The purpose of including this reference is to support the code of the current method to achieve dynamic linking

Method call:

  • Static link: When the bytecode file is loaded into the JVM, the called method can be known at compile time
  • Dynamic link: When the bytecode file is loaded into the JVM, the called method is not known at compile time but known at runtime

Corresponding binding mechanism: Binding is a process in which a symbolic reference to a field, method, or class is replaced with a direct reference, which occurs only once

  • Early binding: the target method is known at compile time and remains unchanged at runtime
  • Late binding: the compilation period cannot be determined, and the related methods are bound according to the actual type when the program is running

Virtual and non-virtual methods:

  • Non-virtual method:
  1. The calling version is determined at compile time, and it is immutable at runtime
  2. Static methods, private methods, final methods, instance constructors, and parent methods are all non-virtual methods
  3. Other methods are called virtual methods

  • virtual method table

 2.3.4, method return address

        Store the value of the PC register (the value of the next instruction called by the method) that calls the method

Guess you like

Origin blog.csdn.net/weixin_44302046/article/details/128685825