Java virtual machine (1): stack frame structure

1, java stack

The first thing to say is: the heap is the heap, the stack is the stack, and the stack is the stack.

The JVM specification stipulates that each Java thread has its own independent JVM stack, which is the call stack of the Java method. It also stipulates that each Java thread has its own independent native method stack. (Remember, this is just the norm, not every thread has to have two stacks).

 

2, the content stored in the java stack frame

In the conceptual model, a typical stack frame is mainly composed of a local variable table (Local Stack Frame), an operand stack (Operand Stack), a dynamic link (Dynamic Linking), and a return address (Return Address).

   local scalar table

    It is a storage space for a set of variable values, used to store  method parameters  and  local variables . The max_locals of the Code attribute of the method table of the Class file specifies the maximum size of the local variable table required by the method.

Variable slot (Variable Slot) is the smallest unit of the local variable table, there is no mandatory size of 32 bits, although 32 bits are enough to store most types of data. A Slot can store 8 types of boolean, byte, char, short, int, float, reference and returnAddress. The reference represents a reference to an object instance, through which the index of the starting address of the object stored in the Java heap and the type information of the data type to which the data belongs in the method area can be obtained. returnAddress points to the address of a bytecode instruction. For 64-bit long and double variables, the virtual machine allocates two consecutive Slot spaces.

The virtual machine uses the local variable table by indexing . We know before that the local variable table stores method parameters and local variables. When the calling method is a non-static method, the slot at the 0th index in the local variable table is used to pass the reference of the object instance to which the method belongs by default, that is, the object pointed to by the "this" keyword. After the method parameters are assigned, the local variables defined inside the method are in turn assigned.

To save stack frame space, Slots in the local variable table can be reused. After leaving the scope of some variables, the slots corresponding to these variables can be handed over to other variables for use. This mechanism sometimes affects garbage collection behavior (such as in a method new a large variable,).

The table structure is as follows:

Tables can contain: pointers to primitives and objects.

   operand stack

The Operand Stack, also known as the Operation Stack, is a last-in, first-out stack. The max_stacks in the Code attribute of the Class file specifies the maximum stack depth during execution. The interpretation execution engine of the Java virtual machine is called a " stack-based execution engine ", where the stack refers to the operand stack.

Arithmetic operations during method execution or when calling other methods for parameter transfer are performed through the operand stack.

In the conceptual model, the two stack frames are independent of each other. But most virtual machine implementations optimize so that the two stack frames partially overlap . Let the lower part of the operand stack overlap with the upper local variable table, so that part of the data can be shared when the method is called, and there is no need to copy and pass additional parameters.

dynamic link

Each stack frame contains a reference to the method to which the stack frame belongs in the runtime constant pool. This reference is held to support Dynamic Linking during method invocation .

A large number of symbolic references are stored in the Class file, and the method call instruction in the bytecode takes the symbolic reference pointing to the method in the constant pool as a parameter. Some of these symbolic references are converted into direct references during the class loading phase or when they are used for the first time. This conversion is called static resolution . The other part will be turned into a direct reference during each run, this part is called dynamic linking .

 

method return address

  • Method return address:
    • When a method is executed, there are two ways to exit the method: the execution engine encounters any bytecode instruction returned by the method or encounters an exception, and the exception is not handled in the method body. No matter what exit method is used, after the method exits, it needs to return to the location where the method was called before the program can continue to execute. When a method returns, it may need to save some information in the stack frame to help restore the execution state of its upper-level method. Generally speaking, when the method exits normally, the value of the caller's PC counter can be used as the return address, which is likely to be saved in the stack frame. When the method exits abnormally, the return address is determined by the exception handler. , this part of the information is generally not saved in the stack frame.
    • The process of method exit is actually equivalent to popping the current stack frame, so the possible operations when exiting are: restore the local variable table and operand stack of the upper-level method, and if there is a return value, push it into the caller's stack frame On the operand stack of , adjust the value of the PC counter to point to an instruction after the method call instruction.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324973102&siteId=291194637