JVM virtual machine tuning actual combat (1) concept articles

JVM virtual machine

1. What language is jvm written in

c and c++;


2. JVM is a merged version of Sun HostSport VM and BEA JRockit VM

This explains why jdk1.8 changed the Java virtual machine permanently to Metaspace. Oracle acquired Sun HostSport VM and BEA JRockit VM. In the course of the struggle between these two virtual machines within Oracle, BEA JRockit VM Victory, and the father of java leaves Oracle.

Here is an explanation of the concepts of metaspace, permanent generation, and method area: The
"Java Virtual Machine Specification" only specifies the concept of a method area and its role, but does not specify how to implement it. Then, the implementation of the method area on different JVMs must be different. At the same time, most of the JVM used is Sun's HotSpot. Extend GC generational collection to the method area on HotSpot, or use permanent generation (meta space) to implement the method area. Therefore, we have come to the conclusion that the permanent generation (metaspace) is the concept of HotSpot, the method area is a definition in the Java virtual machine specification, which is a specification, and the permanent generation (method area) is an implementation, one is a standard Is achieved.


3. The three main subsystems of JVM and their steps

  1. Class loader subsystem
  2. Runtime data area (memory structure)
  3. Execution engine
    Insert picture description here

Action steps: The first step is to load the XXX.class file into the memory, the second part loads some object information into the runtime data, and the third part goes to the execution engine to execute the java program, which means that garbage will be generated here. Collector collection.


4. JVM briefly describes the role of each part of the runtime data area

Method area: shared by threads, which is defined in the Java virtual machine specification. The method area is a logical part of the heap (internally it contains class information, constants, static variables, and code compiled by the JIT compiler) that have been loaded by the virtual machine.

Heap: Shared by threads, it is the memory space used to store objects. Almost all objects are stored in the heap.

Java stack: thread private, a memory model that describes the running process of Java methods. The stack frame is contained inside, and the stack frame contains (local variable table, operand stack, dynamic link, method export information)

Native method stack: Thread private, space prepared for JVM to run Native methods. Since many Native methods are implemented in C language, it is usually called C stack. It is similar to the function implemented by the Java virtual machine stack, except that the local method stack is a memory model that describes the running process of the local method. In the early Java, in order to adapt to the C language, the C language was more popular than Java in the early days, so it is necessary to adapt to the C and C++ languages, but now it is used less.

Program counter: private to the thread. The program counter is a small memory space and is the address of the bytecode instruction currently being executed by the thread. If the current thread is executing a local method, then the program counter is Undefined at this time. The function is (1) The bytecode interpreter reads instructions in turn by changing the program counter, thereby realizing code flow control. (2) In the case of multithreading, the program counter records the position of the current thread execution, so that when the thread switches back, you know where the thread executed last time.

4. JVM briefly describes the Java stack of the runtime data area

Stack model and its role:

Insert picture description here

Note: JVM will allocate a stack frame for each method of a thread


Example (to understand the running process of the entire java stack through examples):

(1) java sample code:


public class Math  {
    
    


    public int math(){
    
    
        int a =1;
        int b=2;
        int c=a+b;
        return c;
    }

    public static void main(String[] args) {
    
    
        Math math = new Math();
        System.out.println(math.math());
    }
}

(2) Compile the javac command to get the bytecode file
(3) Use the javap command to disassemble the .class file so that we can understand the commands executed by the jvm heap memory

javap -c Math.class > Math.txt

This will get the file obtained after disassembly

Compiled from "Math.java"
public class Math {
    
    
  public Math();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public int math();
    Code:
       0: iconst_1
       1: istore_1
       2: iconst_2
       3: istore_2
       4: iload_1
       5: iload_2
       6: iadd
       7: istore_3
       8: iload_3
       9: ireturn

  public static void main(java.lang.String[]);
    Code:
       0: new           #2                  // class Math
       3: dup
       4: invokespecial #3                  // Method "<init>":()V
       7: astore_1
       8: getstatic     #4                  // Field java/lang/System.out:Ljava/io/PrintStream;
      11: aload_1
      12: invokevirtual #5                  // Method math:()I
      15: invokevirtual #6                  // Method java/io/PrintStream.println:(I)V
      18: return
}

(4) To understand these instruction files, use the jvm instruction set here
Insert picture description here
(5) The instruction file Math.txt formed by parsing

  1. First, you will find that the JVM has taken the math() method first. The first instruction of math() is 0: iconst_1(the instruction can be compared with the instruction set above), which 0: iconst_1means that the constant value will be pushed onto the top of the operand stack first. As shown:Insert picture description here
  2. 1: istore_1The instruction is to pop 1 of the operand stack, and then enter the value of 1 into the local variable table and assign it to the variable a. As shown in the figure: Insert picture description here
    3. 2: iconst_2 3: istore_2Do the same principle as step one and step two, as shown in figure:Insert picture description here

4. 4: iload_1,This instruction means to take, for example, a=1, which is the first value, from the local variable table, and then push it to the operand stack ( 5: iload_2同理):Insert picture description here
Insert picture description here

5. 6: iaddAdd the two numbers at the top of the operand stack, and then push the result to the top of the stack Insert picture description here
6. 7: istore_3 8: iload_3 9: ireturnThat is, the value of 3 is popped out of the stack and then placed in the variable table and copied to c, and the result is returned


Summarize the parts of the stack frame:
   local variable table: store some local variables of the method.
   Operand stack: store some temporary numbers for operation;
   dynamic link: for example, the local variable Math is stored in the main() method, and Math refers to the example of the meta space. And this reference is a dynamic link. As shown:
Insert picture description here

   Method exit: record the execution position of the next method and the number of the program counter;

5. JVM briefly describes the method area of ​​the runtime data area

Explanation: Shared by threads, it stores all the fields and method bytecodes of the class. Simply put, all the information defining the method is stored in this area (static variable + constant + class information (construction method + interface definition) + Runtime constant pool), alias Non-Heap (non-heap)

6. JVM briefly describes the heap of the runtime data area

Heap: Heap Insert picture description here
Garbage Collection: Garbage collection will first reclaim the Eden area of ​​the new generation (here YGC), and reclaim objects that are not referenced. If the Eden area is garbage collected, the surviving objects will first be placed in the Surivor space. Area, when the garbage collection (YGC) is performed again, objects that are not referenced in the Eden and From areas will be recycled. If the Eden and From areas are garbage collected, the surviving objects will first be placed in the To area of ​​the Survivor space. This time After the garbage collection is completed, the original From area is converted to the To area, and the To area is converted to the From area. The next time it is recycled, the original To area (in fact, the converted From area) and the Eden area will be recycled again. , Repeat this 15 times, if the object is still alive, then put it into the old age, if the old age also exceeds the threshold set in the pre-configuration file, then it will trigger the Full GC, the trigger of the Full GC will affect the entire Service, leading to service paralysis, so the tuning is generally tuned Full GC.

7. JVM class loading subsystem

1. Class loading process
Insert picture description here

Class loading: The class loader loads the class file into the memory of the virtual machine.
   Loading: Find it on the hard disk and read the bytecode file through IO.
   Connection: Perform verification, preparation, and analysis (optional) Step
   Verification: Checksum The correctness of the code file
   preparation: allocate memory for the static variables of the class and assign default values.
   Analysis: the class loader loads all other classes referenced by the class.
   Initialization: initializes the static variables of the class to specified values and executes the static code Piece

2. Types of class loaders

   Startup class loader: responsible for loading the core class libraries of JRE, such as rt.jar, charsets.jar under the jre target,
   extended class loader: responsible for loading the JAR class package in the JRE extension directory ext
   System class loader: responsible for loading the ClassPath path The class package under (load your own program)
   user-defined loader: responsible for loading the class package under the user-defined path

3. The class loading mechanism (two types) is
fully responsible for the delegation mechanism: when a ClassLoader loads a class, unless another ClassLoader is displayed, the classes that the class depends on and reference are also loaded by this ClassLoader (not commonly used), examples As shown:
Insert picture description here

Parental delegation mechanism: first entrust the parent class loader to find the target class, and find and load the target class in its own path if it is not found.
Insert picture description here
4. Advantages of the parental delegation mode

Sandbox security mechanism: The String.class class written by yourself will not be loaded, so that you can prevent the core API library from being tampered with at will. First of all, the String class belongs to the class loaded by the boot class recorder. If the String.class written by yourself is loaded, this class will be arbitrarily modified, exposing security risks. The parent delegation mechanism will avoid this situation, because the String class written by yourself will not be loaded, but will be loaded by the boot class loader, thus avoiding the String written by yourself from being loaded.

Avoid repeated loading of the class: when the father has already loaded the class, there is no need to load the child ClassLoader again

5. Will JVM load jar package load all the classes in the package into memory?

No, JVM loads class files on demand (dynamic loading during runtime), not one-time loading.

Guess you like

Origin blog.csdn.net/qq_41133245/article/details/108938586