This is the second day of my participation in the More text Challenge. For more details, see more text Challenge

This article is participating in the “Java Theme Month – Java Development in action”. See the link to the event for more details

PC register

The heap area and metadata area, or method area (including CodeCache), are shared by threads, while the rest of the area is thread exclusive.

The stack is mainly runtime memory, and the heap is storage-time memory.

Overview: THE PC register is an abstract simulation of the physical CPU register, mainly used to store instruction related information, namely the address of the next instruction to be executed, by the execution engine to read the next instruction. – GC and OOM will not occur.

Function: Records the next byte code instruction to be executed. Is the program flow indicator, exception handling, thread recovery and other operations are dependent on the program counter to complete.

Look at the bytecode file with the javap -v command to see the specific instruction address

Advantage:

In the multi-threaded execution environment, multiple threads switch the CPU execution right back and forth, it is necessary to record the number of execution lines of each thread, so that the CPU can continue to execute from the position of the last execution when switching back. Based on this situation, THE PC register is also thread private, to prevent the counter from being changed by other threads.

The virtual machine stack

1. An overview of the

Each thread is created on a virtual machine stack, which is thread private. The stack stores several stack frames, and each stack frame corresponds to a Java method. The life cycle of the stack is the same as that of the thread. There are only two operations of the stack, the method of the execution of the stack, the end of the execution of the stack, the efficiency of the stack is second only to the PC register. GC does not exist, but OOM exists.

2. The role

Responsible for running Java programs, saving local variables and partial structures of methods, participating in method invocation and return.

3. Stack memory Settings

By default, the stack memory changes dynamically, and you can use the -xss parameter to set the stack size.

4. How the stack works

In A thread is the same point in time, there can be only one stack frame in the activity, namely the currently running method corresponding stack frame, and the stack, if method A calls method B, then the method A corresponding frame into the stack, and then calls the method B, method B corresponding frame into the stack, this method B is the current stack frame, If method B has an exception that is not handled, then the current stack frame will be ejected. The exception will be returned to method A. If method A also does not handle, then the current stack frame will be ejected. ** Note: ** stack frames of different threads may not call each other.

5. Stack frame internal structure

The stack frame consists of the following five parts:

  • Local variation scale:

    • Is an array of numbers that mainly stores method parameters, local variables of the method, and return values.
    • The size of a locally changed table is determined by the compiler and does not change at method run time. The size of the stack frame depends mainly on the local variable scale.
    • The basic storage unit of the local variable table is Slot. Types with less than 32 bits occupy one Slot, and types with 64 bits occupy two slots (long and double). Each Slot has an index that points to the value of the local variable table. If the method was created by a constructor or instance method (a non-static method), then index[0] holds the this object. Slots can be reused. If a variable is out of scope, the Slot will be used by the following variables.
    • The variables in the local variables table are important garbage collection root nodes, as long as the objects directly or briefly referenced in the local variables are not reclaimed.
  • ** Operand stack: ** During the execution of a method, data is stored (pushed) and extracted (extracted) from the operand stack according to bytecode instructions. It mainly stores intermediate results and intermediate variables in the calculation process, as well as return values.

  • Dynamic linking: A method reference to a pool of runtime constants. The function of a dynamic linking is to convert a symbolic reference to a direct reference to the calling method.

  • Method return address: The value of the PC register in which the method is stored when the method exits and returns to where the method was called. An exception return does not return any return value.

  • Additional information (negligible) : Improved support for program debugging.

Look inside the stack in terms of code and bytecode

public int getSum(a){
    int m = 10;
    int n = 20;
    int k = m + n;
    return k;
}
/** * check the byte code * 0 /** * check the byte code * 0 /** * check the byte code * 0 /** * check the byte code * 0 * 2 istore_1 registers the pointer position 2 - operands stack out of the stack, stored in the local variable table at index 1, because the method is non-static, so stored at index 0 this, * 3 bipush 20 * 5 istore_2 * 6 iload_1 retrieve index 1 from local variable table and store it in operand stack * 7 iload_2 The iADD instruction needs to be translated by the execution engine into the machine instruction, and then the CPU performs the operation and stores the result in the operand stack * 9 istore_3 the operand stack takes the top element, stores the local variable table index 3 * 10 iloAD_3 takes the element index 3 from the local variable table, Save the operand stack * 11 iRETURN Pops from the operand stack and returns */
Copy the code

6. Method binding mechanism

A binding is a field, method, or class that is converted from a symbolic reference to a direct reference only once.

Early binding: The target method is known at compile time and remains unchanged at run time, that is, a symbolic reference is converted to a direct reference using static linking.

Late binding: The method being called cannot be determined at compile time, but can only be converted from symbolic references to direct references at run time using time-dynamic linking based on the actual type of binding associated methods.

7. Virtual and non-virtual methods

If the method compiler determines the exact version of the call, and the method is immutable at the time of the call, then the method is non-virtual. For example, static methods, private methods, final-modified methods, instance constructors, and superclass methods are all non-virtual, and all others are virtual. A call to a virtual method that occurs because of a method override.

Related instructions:

  • Invokestatic: Static method to invoke, not virtual. Except for the final modifier
  • Invokespecial: Calls init(), private, superclass methods, non-virtual methods. Except for the final modifier.
  • Invokevirtaul: Calls all virtual methods.
  • Invokeinterface: Invokes interface methods, virtual methods
  • Invokedynamic: A method that dynamically parses calls, primarily lambad

Virtual method table:

Each method has a virtual method table that holds the actual entry for each method. Virtual methods look up each time they are called (dynamic dispatch). To improve performance, the JVM creates a table of virtual methods in the method area of the class and uses an index table instead of looking up. Virtual method tables are created during parsing during the link phase of class loading.