当前位置:网站首页>JVM (6): slot variable slot, operand stack, code trace, stack top cache technology

JVM (6): slot variable slot, operand stack, code trace, stack top cache technology

2022-06-11 04:17:00 Prince you

One 、Slot Understanding of variable slots

         Parameter values are always stored in the local variable array index0 Start , To array length -1 End of index of .

         Local variable table , The most basic storage unit is slot( Variable slot )

         The local variable table stores various basic data types known at compile time (8 Kind of ), Reference type (reference), returnAddress Variable of type .

         In the local variable table ,32 Types within bits occupy only one slot( Include returnAddress type ),64 Bit type (long and double) Take two slot.

  • byte 、short 、 char Converted to before storage int,boolean Also converted to int,0 Express false , Not o Express true.
  • long and double They occupy two slot.

 Slot Reuse of waste

         The slots in the local variable table in the stack frame can be reused , If a local variable goes beyond its scope , Then the new local variable declared after its scope is likely to reuse the slot of the expired local variable , So as to save resources .

public class SlotTest {
    public void localVal1() {
        int a = 0;
        System.out.println(a);
        int b = 0;
    }

    public void localVal2() {
        {
            int a = 0;
            System.out.println(a);
        }
        //  here b It will be reused a Slot of 
        int b = 0;
    }
}

Two 、 The comparison between static variables and local variables

         After the parameter list is assigned , According to the order and scope of the variables defined in the method body .

         We know that class variable table has two initialization opportunities , For the first time “ Preparation stage ”, Perform system initialization , Set a zero value on a class variable , The other was in “ initialization ” Stage , Give the programmer the initial value defined in the code .

         Unlike class variable initialization , There is no system initialization process in the local variable table , This means that once a local variable is defined, it must be artificially initialized , Otherwise it won't work .

    public void test(){
        int i;
        System.out.println(i);
    }

        This code is wrong , Cannot use without assignment .

Additional explanation

         In the stack frame , The most relevant part of performance tuning is the local variable table mentioned earlier . When the method executes , The virtual machine uses the local variable table to complete the method transfer .

         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 are not recycled .

3、 ... and 、 The stack of operands

         Each independent stack frame contains a list of local variables , One in and one out (Last-In-First-out) The stack of operands , It can also be called an expression stack (Expression stack) .

         The stack of operands , During method execution , According to bytecode instruction , Write or extract data from the stack , That's the stack (push)/ Out of the stack (pop).

  • Some bytecode instructions push values into the operands stack , The rest of the bytecode instructions take the operands out of the stack . Use them and push the results on the stack .
  • such as : Perform replication 、 In exchange for 、 Sum and so on

         The stack of operands , It is mainly used to save the intermediate results of the calculation process , At the same time, it serves as the temporary storage space for variables in the calculation process .

         The stack of operands is JVM A workspace of the execution engine , When a method is first executed , A new stack frame will also be created , The operand stack of this method is empty .

         Each stack of operands has a clear stack depth for storing values , The maximum depth required is defined at compile time , Save in the method of code Properties of the , by max_stack Value .

         Any element in the stack can be arbitrary Java data type .

  • 32bit The type occupies a stack unit depth
  • 64bit The type of the stack occupies two stack units of depth

         The operand stack does not use the way of access index to access data , But only through the standard entry branch (push) And out of the stack (pop) Operation to complete a data access .

         If the called method has a return value , Its return value will be pushed into the stack of operands in the current stack frame , And update the PC The next bytecode instruction to be executed in the register .

         The data type of the elements in the operand stack must strictly match the sequence of bytecode instructions , This is verified by the compiler during the compiler , At the same time, in the class loading process, the data flow analysis phase of the class verification phase needs to be verified again .

         in addition , We said Java The interpretation engine of virtual machine is stack based execution engine , The stack refers to the operand stack .

Four 、 Code tracking

 ​​​​​

  5、 ... and 、 Stack top caching technology

          I mentioned it earlier , Virtual machines based on trestle architecture use more compact zero address instructions , But when completing an operation, it is necessary to use more stack in and out instructions , This also means that more instruction dispatch will be required (instruction dispatch) Times and memory reads / Write times .

         Because the operands are stored in memory , Therefore, memory reads are performed frequently / Write operations will inevitably affect the speed of execution . To solve this problem ,HotSpot JVM The designers of the stack top cache (Tos,Top-of-stack Cashing) technology , Cache all stack top elements in physical cPu In the register of , In order to reduce the memory read / Write times , Improve the execution efficiency of the execution engine .

原网站

版权声明
本文为[Prince you]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206110405099447.html