当前位置:网站首页>JVM -- class loading process and runtime data area

JVM -- class loading process and runtime data area

2022-07-04 07:28:00 Mingvvv

Compile and decompile

compile

javac -eancoding utf-8 ./Test.java
Specify the encoding format , Generate Test.class Bytecode file , Not easy for developers to interpret
Bytecode files are JVM Identify and run , If it is necessary to facilitate developers to view the corresponding contents of the compiled bytecode file , You can use the following instructions

Decompile

javap -p -v ./Test.class > ./Test.txt
View all instruction information of class file , Bytecode will compile the assembly into a convenient format .

Class loading

load link initialization
verification ⇨ Get ready ⇨ analysis

Class file -> Byte stream -> Class loader

load

  1. Store the compiled bytecode file in the method area
  2. Create a in the heap java.lang.Class The object of , To serve as an access to data in the method area

link

verification

Verify the file format of bytecode file 、 data 、 Whether the symbol reference is correct

Get ready

Allocate memory for static variables of a class

analysis

Turn symbolic references into direct references .
Symbol references are stored in the running time pool .
Symbol references in class There are only some descriptions in the document , You need to open up a piece of its corresponding real physical memory in the virtual machine

Class loader

loader function
BootStrap Classloader load $JAVA_HOME/jre/lib/rt.jar All in class perhaps -Xbootclasspath Option specified jar package ,
ExtClassLoader load Java Some of the extensions in the platform jar package , Include $JAVA_HOME/jre/lib/*.jar or -Djava.ext.dirs Under the specified directory jar package
AppClassLoader load classpath Specified in the jar Package and -Djava.class.path The classes in the specified directory and jar package
Custom ClassLoader adopt java.lang.ClassLoader Custom loading of subclasses class , It belongs to the application customized according to its own needs ClassLoader, Such as tomcat,jboss Will be based on j2ee Norms are self actualized ClassLoader

example

public class Test
{
    
    public static void main(String[] args) {
    
        //classpath Class loader under  AppClassLoader
        System.out.println(new Test().getClass().getClassLoader());
        //ExtClassLoader
        System.out.println(new Test().getClass().getClassLoader().getParent());
        //BootStrap Classloader c Language implementation ,java It cannot be displayed on the level 
        System.out.println(new Test().getClass().getClassLoader().getParent().getParent());
    }
}

 Print the results 
sun.misc.Launcher$AppClassLoader@18b4aac2
sun.misc.Launcher$ExtClassLoader@39a054a5
null

Parent delegate mechanism

 Insert picture description here
If the current loader has a parent loader , Delegate to the parent loader to find the class object , Up to the top floor . This class does not exist in the parent class , You have to find and load it yourself , If you can't find this class , Then return to ClassNotFoundException Type error .

Run time data area

 Insert picture description here

Method area

Save the type information that has been loaded by the virtual machine 、 Constant 、 Static variables 、 Timely compiled code cache and other data .
The method area is created when the virtual machine starts , All threads share , Life cycle and java Process bound .
The runtime constant pool is in the method area

Pile up

Store object information , Thread sharing

Java Stack

Java Stack frames are stored one by one , Each stack frame corresponds to a called method , Include local variable table in stack frame 、 The stack of operands 、 Dynamic links 、 Method and some additional information . When a thread executes a method , A corresponding stack frame will be created , And stack the established stack frame . When the method is finished , Then the stack frame will be out of the stack . So we know , The stack frame corresponding to the method currently executed by the thread must be located Java The top of the stack .

Dynamic links : A reference to the runtime constant pool of the class to which the current method belongs . Symbolic references are converted to direct references during initialization or first use . Symbolic reference refers to the compilation process , Referenced variables 、 Method 、 Classes and other information have not been loaded into the virtual machine , Therefore, it can only be stored in class In the constant pool of files . Dynamic linking is to convert these symbolic references into direct references that can be called .

The stack of operands : During the instruction , The copied constants or variables in the fields of the local variable table or object instance will be written to the operand stack , Then stack the elements in the stack to the local variable table or return them to the method caller with the calculation .

Method return address : Store the value of the program counter calling the method . Method execution , The address of the next instruction of the caller of the method will be returned .

Native Method Stack

The local method stack serves to execute local methods . stay JVM Specification , There is no mandatory requirement for the implementation method and data structure of local method stack , Virtual machines are free to implement it . stay HotSopt In the virtual machine, the local method stack and Java Stack as one .

Program counter

Also called PC register , Save the address of the instruction currently executed by the program ( It can also be said that the address of the storage unit where the next instruction is stored ), When CPU When an instruction needs to be executed , You need to get the address of the storage unit where the current instruction to be executed is from the program counter , And then get the instructions according to the obtained address , After getting the order , The program counter will automatically add 1 Or get the address of the next instruction according to the transfer pointer , So circular , Until all instructions have been executed .
Because a CPU Only one instruction can be processed at a time , Therefore, the implementation of multithreading is essentially to switch frequently between multiple threads to achieve parallel implementation . By recording the address of the next instruction of the current thread , Thus, when switching back to the current thread JVM Be able to know what instructions to execute .

原网站

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