My JVM study notes: runtime data area (2) detailed local variable table

Thanks to Mr. Song Hongkang from Shang Sigu for the JVM entry to the proficient course, and salute every teacher who teaches free courses attentively!
This set of tutorials are all my study notes after the course, to prevent forgetting, and send to everyone to share, thank you for viewing~

1. Detailed explanation of the virtual machine stack

This chapter is connected to the previous article: My JVM Study Notes: Chapter 3-Runtime Data Area (1) , please click to view if necessary.

The internal structure of the stack

The stack contains:

  • Local variables
  • Operand Stack (or expression stack)
  • Dynamic Linking (or method reference to runtime constant pool)
  • Method return address (Return Address) (or the definition of the method exits normally or abnormally)
  • Some additional information
    Insert picture description here
    Note: The stack frame contains all the information of the method, the stack frame also has a size, the smaller the stack frame, the more methods can be executed at the same time

Local scale

Also called: local variable table, local variable array.

So why is it called an array of local variables?

Because: the local variable table is defined as aNumeric array, Mainly used for storage methodsParameters and local variables defined in the method body, These data types include various basic data types, object references (reference), and returnAddress (return value).

Note: Because the local variable table is built on the thread's stack and is the thread's private data, there is no data security issue.

And: the required capacity of the local variable table is inCompile timeDetermined and saved in the maximun local variables data item of the Code attribute of the method. In methodWill not change during operationThe size of the local variable table.

test:

public static void main(String[] args) {
    
    
    String a = "aaaa";
    int b = 1;
    float c = 1.1f;
    boolean d = true;
    Object f = new Object();
}

Test command:

javap -v 

Result: The
local variable table is determined after the compilation is completed, including all variables in the method (including method parameters).
Insert picture description here

Note: The
variables in the local variable table are only inValid in current method call. When the method is executed, the virtual machine is completed by using the local variable tableTransfer process of parameter value to parameter variable list. When the method call ends, as the method stack frame is destroyed, the local variable table will alsoThen destroyed

And: Because the local variable table generally stores more information, the local variable table is one of the main factors that determine the size of the stack frame!

Variable slot (Slot)

Local variable tableThe most basic storage unitIt is the variable slot.

The storage of parameter values ​​always starts at index0 of the local variable array and ends at the index of array length-1.
In the local variable table,Types within 32 bits only occupy one slot (Including returnAddress type),64-bit types (long and double) occupy two slots.
note:Sort, byte, boolean and other data also occupy a variable slot, Because jvm will convert the above variables to int type when storing (variable slot is the most basic storage unit, can not be divided, can only be used as a whole).

In other words: the local quantity meter is actually an array of variable slots, each variable slot can store 32-bit data, the array with less than 32 bits is automatically converted to 32-bit padding, 64-bit data will occupy two variable slots, local variables The table will record the data stored in the index position of the variable slot.

Insert picture description here
The JVM will allocate a variable slot for each variable in the local variable table and record its storage location. For example, the variable args is stored in the variable slot with index 0, and the variable c is 64 bits, so it needs to occupy two variable slots (3 And 4), because C occupies two variable slots, variable D is stored directly from index5.
Insert picture description here

Note:
If the current method is an instance method (or a constructor method), the JVM will create a this variable and store it at index0 by default!

public class JvmTest {
    
    
    public JvmTest()
    {
    
    
        System.err.println("这是构造方法!");
    }
    public String sayHello(String name)
    {
    
    
        return name + "说Hello";
    }
}

Insert picture description here
Therefore, we call this in the method because the JVM creates this variable for us by default, so we can call it directly.

Note:
Variables that are not assigned (assigned in the code block) will not be displayed in the local variable table, but the local variable table will still reserve a place for it.
Insert picture description here
As you can see, the variable b has no initial value, and the variable c is assigned in the code block, but the static variable table is not displayed.
Insert picture description here
But please note that the local variable table still reserves a place for it to facilitate the use of variables.
Insert picture description here
Only the displayed value assigned to the variable can be displayed in the local variable table.

Multiplexing of variable slots

Insert picture description here
Looking at our code here, we can find that we have declared variables a, b, c, but there are only three local variables this, a, and c in the local variable table.
Insert picture description here
The length of the actual local variable table is only 3 (plus this should have 4).

The reason here is that the JVM has a reusability for variable slots. When the variable b exceeds its scope, it will no longer take effect, so the variable c directly occupies the position of b, so there will be one less position in the local variable table.

to sum up:

The local variable table only records the variables (and not in the code block) and method parameters that have been determined to have certain values. They can be used directly during program execution and stored in the variable slot. If it is long and double, it needs to occupy two Variable slot, data with insufficient length will fill a variable slot, instance method and construction method will automatically create this variable, and if the code block ends (end of scope), jvm will have a multiplexing behavior for the variable slot, In order to save space.

Supplement: The
variables in the local variable table are also important garbage collection root nodes, as long as the objects directly or indirectly referenced in the local variable table will not be recycled.

The knowledge points here will be analyzed later~

Thank you for watching. This article is my study notes. If there is any mistake, please correct me~

Guess you like

Origin blog.csdn.net/qq_42628989/article/details/107444352