JVM---virtual machine stack (local variable table)

Virtual machine stack-local variable table

Local Variables Table (Local Variables): It is called local variable array or local variable table.

  • The local variable table is defined as a numeric array, which 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, and returnAddress types.
  • Since the local variable table is built on the thread's stack and is the thread's private data, 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 will not be changed during the running of the method.
  • The number of nested method 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 is, the larger its stack frame will be, and the more stack space the function call will occupy, which will lead to the number of nested calls. cut back.
  • 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 parameter values ​​to the parameter variable list by using the local variable table. After the method is called, the local variable table is also destroyed as the method stack frame is destroyed.

Slot

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

Insert picture description here
Slot reuse

The slots in the local variable table in the stack frame can be reused. If a local variable exceeds 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.

Insert picture description here
Comparison of static variables and local variables

Classification of variables:

According to data types: basic data types, reference data types.

According to the position declared in the class: member variables (class variables, instance variables), local variables.

Class variables: In the paper phase of linking, class variables are assigned by default, and in the init phase, they are displayed and assigned static code blocks.

Instance variable: As the object is created, instance variable space will be allocated in the heap space and assigned by default.

Local variables: must be explicitly assigned before use, otherwise the compilation will not pass.

  • After the parameter list is allocated, it is allocated according to the order and scope of the variables defined in the method body.
  • The class variable table has two opportunities to initialize, the first time is in the "preparation phase", the system initialization is performed, and the zero value is set for the class variables, and the other time is in the "initialization" phase, giving the programmer the initial definition in the code value.
  • Unlike the initialization of class variables, there is no system initialization process in the local variable table, which means that once the local variable is defined, it must be initialized artificially, otherwise it cannot be used.
  • In the stack frame, the part most closely related to performance tuning is the local variable table. When the method is executed, the virtual machine uses the local variable table to complete the method transfer.
  • 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.

Guess you like

Origin blog.csdn.net/qq_33626996/article/details/113827277