How does JVM load bytecode files?

 After the Java compiler has compiled the .class file, we need to use the JVM to run the class file. Then the first job is to import the bytecode from the disk into the memory. This process is called [ load  ]. After the loading is complete, we can perform a series of pre-run preparations, such as: opening up space for class static variables, storing the constant pool in the method area memory and implementing constant pool address resolution, initializing class static variables, and so on. In this article, we are going to talk about how the JVM loads class files?

Class loading mechanism

The life cycle of a class from being loaded into the virtual machine memory to unloading the memory includes: loading -> connection (verification -> preparation -> analysis) -> initialization -> use -> uninstall

image


  • Load

    • Load the bytecode content of the class file into the memory, and convert these static data into the runtime data structure in the method area, and generate a java.lang.Class object representing this class in the heap as the method area class data Access to the entrance, this process requires the participation of class loader.

  • Link     the process of merging the binary code of the java class into the running state of the JVM

    •   Verification: Ensure that the loaded class information complies with the JVM specification, and there is no security problem


    •   Preparation: The stage of formally allocating memory for class variables ( static variables) and setting the initial value of class variables. These memories will be allocated in the method.


    •   Analysis: The process of replacing symbol references in the virtual machine constant pool with byte references

  • initialization

    • The initialization phase is the process of executing the <clinit> () method of the class constructor . Class constructor <the clinit> () method is implemented by the operation of the automatic assignment compiler collection classes and static variables of all classes statement blocks (static block ) statements merger

    • When initializing a class, if you find that its parent class has not been initialized, you need to trigger the initialization of its parent class first

    • The virtual machine ensures that the <clinit> () method of a class is correctly locked and synchronized in a multi-threaded environment

    • When the static domain of a Java class is scoped , only the class that truly claims this domain will be initialized

The java loading class uses the "full responsibility delegation mechanism":

It means that when a ClassLoader loads a class, unless another ClassLoader is used, the classes that the class depends on and reference are also loaded by this ClassLoader. The "delegation mechanism" refers to first entrusting the parent class loader to find the target class, and only find and load it from its own path when it cannot be found. This point is considered from the security aspect. Just imagine if someone writes a malicious basic class (such as java.lang.String) and loads it into the JVM, it will cause serious consequences, but the overall responsibility, java.lang.String will always be It is loaded by the root loader to avoid the above situation.


Guess you like

Origin blog.51cto.com/15082402/2644355