JVM runtime data area-virtual machine stack

Table of contents

1. Stack in memory

2. Basic content

3. Advantages

4. Storage unit of stack

5. Stack operation principle

6. Internal structure of the stack

(1) Local variable table

(2) Operand stack

(3) Dynamic link

(4) Method return address

(5) Additional information

(6) Method call

1. Link method

2. Binding of methods

3. Virtual methods and non-virtual methods

4. Method calling instructions

5. The essence of method rewriting

6. Virtual method table


This time I’m learning about the Java virtual machine stack part in the JVM runtime data area.

1. Stack in memory

The stack is the unit of runtime, and the heap is the unit of storage. The stack determines how the program executes and how to process data. The heap solves the problem of data storage, that is, how and where to put the data.

2. Basic content

  • Java virtual machine stack, also called Java stack in the early days, creates a virtual machine stack when each thread is created, and stores stack frames internally, corresponding to Java method calls one after another.
  • Consistency of life cycle and thread
  • Responsible for the running of Java programs, saving local variables of methods (8 basic data types, reference addresses of objects), partial results, and participating in the calling and return of methods.
    • Local variables vs member variables
    • Basic data types VS reference type variables (classes, arrays, interfaces)

3. Advantages

1. Fast and effective storage method, the access speed is second only to the program counter

2. The JVM has only two direct operations on the JAVA stack.

  • Each method is executed, accompanied by pushing (pushing, pushing)
  • Pop the stack at the end of execution

3. There is no garbage collection on the stack, but there is OOM.

4. The Java stack size is dynamic or fixed. If it is dynamic expansion, sufficient memory cannot be applied for OOM. If it is fixed, and the stack capacity requested by the thread exceeds the fixed value, StackOverflowError will occur.

5. Use -Xss (memory: standing for a small operation, stack Xss) to set the maximum stack space of the thread

4. Storage unit of stack

  1. Each thread has its own stack, and the data in the stack is stored in stack frame format.
  2. Each method being executed on the thread corresponds to a stack frame.
  3. A stack frame is a memory block and a data set that maintains various data information during method execution.
  4. First in, last out, last in, first out
  5. In an active thread, there will only be one active stack frame at a point in time. Only the top stack frame of the currently executing method is valid. This is called the current stack frame. The corresponding method is the current method and the corresponding class is the current class.
  6. All bytecode instructions run by the execution engine only operate on the current stack frame.
  7. If other methods are called in the method, the corresponding new stack frame will be created and placed at the top to become the new current frame.

5. Stack operation principle

Stack frames contained in different threads are not allowed to reference each other.

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, then the virtual machine discards the current stack frame, making the previous stack frame become a new stack frame.

Java methods have two return methods

  • One is a normal function return, using the return instruction
  • The other is to throw an exception. Either way will cause the stack frame to be popped.

6. Internal structure of the stack

Stack frames are stored in the stack, and each stack frame stores:

① Local variable table

② Operand stack

③ Dynamic link

④ Method return address

⑤ Some additional information

(1) Local variable table

1. Defined as a numeric array, mainly used to store method parameters and local variables defined inside the method body. The data types include various basic data types, object references, and return address types.

2. The local variable table is built on the thread's stack and is private to the thread, so there is no data security issue.

3. The capacity of the local variable table is determined at compile time.

4. The local variable table stores various basic data types (8 types) known at compile time, reference types (reference), and return address types.

5. The most basic storage unit is slot. 32-bit types occupy one slot, and 64-bit types (long and double) occupy two slots.

6. The variables in the local variable table are only valid in the current method call. The virtual machine completes the transfer process of parameter values ​​to the parameter variable list by using the local variable table. After the method call is completed, as the method stack frame is destroyed, the local variable table will also be destroyed.

7. Understanding of Slot

  • The JVM virtual machine allocates an access index to each Slot in the local variable table. Through this index, the local variable value specified in the local variable table can be successfully accessed.
  • If the current frame is created by a constructor or instance method, then the object refers to this and will be stored in the slot with index 0, and the rest of the parameter list continues to be arranged in sequence.

Replenish:

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 Local variable table completion method transfer

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

(2) Operand stack

During method execution, data is written to or extracted from the stack according to the bytecode instructions, that is, push/pop

1. If the called method has a return value, its return value will be pushed into the operand stack of the current stack frame, and the program counter will be updated with the next bytecode instruction that needs to be executed.

2. The interpretation engine of the Java virtual machine is a stack-based execution engine, where the stack is the operand stack.

3. It is mainly used to save the intermediate results of the calculation process and also serves as a temporary storage space for variables during the calculation process.

4. When a method first starts executing, a new stack frame will be created. The operand stack of this method is empty.

5. Each operand stack will have a clear stack depth for storing values. The maximum depth is defined at compile time.

6. In the stack, the 32-bit type occupies one stack unit depth, and the 64-bit type occupies two stack unit depths (the same as the Slot storage in the local variable table)

7. The operand stack does not use the access index method for data access. Instead, it can only complete one data access through standard push and pop operations.

8, Stack top caching technology

Since the operands are stored in memory, frequent memory read and write operations affect the execution speed. All the top elements of the stack are cached in the registers of the physical CPU, thereby reducing the number of reads and writes to the memory and improving the execution efficiency of the execution engine.

(3) Dynamic link

A method reference pointing to the runtime constant pool (the runtime constant pool is placed in the method area)

  • Each stack frame contains a reference to the method in the runtime constant pool to which the frame belongs.
  • The purpose is to support the code of the current method to achieve dynamic linking, such as the invokedynamic instruction
  • When the java source file is compiled into a bytecode file, all variables and method references are used as symbol references and stored in the constant pool of the class file.
  • When describing a method that calls another method, it is represented by a symbolic reference pointing to the method in the constant pool.
  • The function of dynamic linking is to convert these symbolic references into direct references to the calling method.

Constant pool, runtime constant pool

  • The constant pool is in the bytecode file, and the runtime constant pool is in the method area of ​​​​the runtime.

(4) Method return address

1. Store the value of the pc register that calls this method

2. End of method

  • Normal execution completed
  • An unhandled exception occurred and exited abnormally.

3. No matter which method you exit, after the method exits, it will return to the location where the method was called. When the method exits normally, the value of the caller's PC counter is used as the return address, that is, the address of the next instruction following the instruction that called the method.

4. For abnormal exit, the return address is determined through the exception table. This part of the information is generally not saved in the stack frame.

5. When the execution engine encounters a bytecode instruction (return) returned by any method, the return value will be passed to the upper method caller, which is referred to as the normal completion exit. Return instructions include:

  • Used when the return value of ireturn is boolean, byte, char, short, and int types
  • lreturn——long
  • dreturn——double
  • areturn - reference type
  • There is also a return pointer for methods declared as void, instance initialization methods, initialization methods of classes and interfaces.

6. In essence, the exit of a method is the process of popping the current stack frame. At this time, it is necessary to restore the local variable table and operand stack of the upper-layer method, push the return value into the operand stack of the caller's stack frame, set the PC register value, etc., so that the caller method can continue to execute.

7. The difference between the normal completion exit and the abnormal completion exit is that exiting through the exception completion exit will not generate any return value to its upper-level caller.

(5) Additional information

It is allowed to carry some additional information related to the Java virtual machine implementation, such as information that provides support for program debugging. Not every stack frame has it, so it is actually correct to say that the stack frame mainly contains local variable tables, operand stacks, dynamic links and method return addresses.

(6) Method call

1. Link method

① Static link

When a bytecode file is loaded into the JVM, if the called target method is known at compile time and remains unchanged during runtime, the process of converting the caller's symbolic reference into a direct reference is called static Link

② Dynamic link

If the called method cannot be determined at compile time, the symbolic reference of the called method can only be converted into a direct reference at runtime. This reference conversion process is dynamic, so it is called dynamic linking.

2. Binding of methods

Binding is the process by which a field, method, or class's symbolic reference is replaced by a direct reference. Only happens once.

① Early binding

The called target method is known at compile time and remains unchanged at run time.

② Late binding

The called method cannot be determined at compile time, and the relevant method can only be bound according to the actual type during program running.

Any ordinary method in Java has the characteristics of a virtual function (confirmed at runtime and has the characteristics of late binding). In C++, the keyword virtual is used to explicitly define it.

If you do not want a method to have the characteristics of a virtual function in a Java program, you can use the keyword final to mark the method.

3. Virtual methods and non-virtual methods

① Non-virtual method

If the method determines the specific calling version at compile time, this version is immutable at runtime. Such methods are called non-virtual methods

Static methods, private methods, final methods, instance constructors, and parent class methods are all non-virtual methods

② In addition to non-virtual methods, other methods are called virtual methods

4. Method calling instructions

① Ordinary calling instructions

  • invokestatic invokes a static method, and the unique method version is determined during the parsing phase
  • invokespecial calls the <init> method, private and parent class methods, and determines the unique method version during the parsing phase
  • invokevirtual calls all virtual methods
  • invokeinterface Call interface method
  • Among them, the methods called by the invokestatic instruction and the invokespecial instruction are called non-virtual methods, and the rest (except those modified by final) are called virtual methods. 

② Dynamic calling instructions

  • invokedynamic dynamically parses the method that needs to be called, and then executes it

It was not until the emergence of Lambda expressions in Java 8 that the generation of invokedynamic instructions had a direct generation method in Java. Java itself is a static language, and dynamic calling instructions give Java the characteristics of a dynamic language.

5. The essence of method rewriting

① Find the actual type of the object executed by the first element on the top of the operand stack, recorded as C

② If a method in type C is found that matches both the descriptor and simple name in the constant pool, access permission verification is performed. If it passes, a direct reference to the method is returned, and the search process ends. If it does not pass, java is returned. lang.IllegalAccessErrorException

③ Otherwise, perform the previous search and verification process on each parent class of C from bottom to top according to the inheritance relationship.

④ If no suitable method is found, a java.lang.AbstractMethodError exception is thrown.

6. Virtual method table

In object-oriented programming, dynamic allocation is used frequently. If each dynamic allocation process requires a new search for a suitable target in the method metadata of the class, it may affect the execution efficiency. Therefore, in order to improve performance, the JVM Create a virtual method table in the method area of ​​the class and use an index table instead of searching

Each class has a virtual method table, and the table stores the actual entry of each method

The virtual method table will be created and initialized during the linking phase of class loading. After the initial values ​​of the class variables are prepared, the JVM will also initialize the methods of the class.

Guess you like

Origin blog.csdn.net/m0_62946761/article/details/133854569