How ClassLoader works

This article mainly introduces the working mechanism of the class loader

One: First of all, what is a class loader?

  The class loader is used to load java classes into the java virtual machine. After the java source program is compiled, a bytecode file is formed, and the class loader loads the bytecode file into the memory and converts it into an instance object of java.lang.Class.

  The JVM is based on stack operations: all operations go through push and pop operations. Advantages of stack-based operations: The optimization work at runtime is combined with the execution engine that performs compile-time optimization, so as to optimize the Java bytecode.

Two: The role of the class loader:

  1. Load the compiled class bytecode file into the JVM. Will the loading process be reviewed which class loader is loaded for each class? Which class to load? There is actually a parent-first hierarchy
  2. Reparse the bytecode file into the object format uniformly required by the JVM.

Three: Classification of class loaders

  •   BootStrap ClassLoader: The loading process is determined by the JVM itself. How to load and which class to load is controlled by the JVM itself. In fact, it does not conform to the JVM specification. There is no hierarchical structure, no parent loader, and no child. Loader. He exists only as a loading tool
  •      ExtClassLoader: Although it is part of the JVM itself, the loading is not loaded by the JVM itself. The specific target it serves is in the System.getProperties("java.ext.dirs") directory
  •      APPClassLoader : is a subclass of ExtClassLoader class loader, mainly loads the classes located in the classpath directory System.getProperties("java.class.path")

 

 

Four: Two ways for JVM to load bytecode files

  1. Implicit loading: Do not call the class loader and automatically load the required classes into memory. For example: when the current class needs a reference to an external class, implicit loading will be triggered
  2. Explicit loading: The loading completed by calling this.getClass(), this.getClassLoader(), Class.forName(class), etc. is an explicit loading

The two loading methods can be mixed, explicitly loading a custom class, and if there are references to other classes in the class, implicit loading will be triggered.

 

 Five: Analysis of common errors in class loading

  • ClassNotFoundException: When the bytecode file is explicitly loaded, the corresponding bytecode file cannot be found. The reason for this problem is that there may not be a corresponding bytecode file in the corresponding classpath, resulting in an exception; solution : Go to the corresponding classpath to check whether there is a corresponding bytecode file, and obtain the path of path through this.getClass().getClassLoader().getResources().toString();
  • NotClassDefFoundError: The reason for the exception that the class does not exist is: the new keyword, the reference class, the inherited interface or class, and the method parameter has a reference, which will lead to the occurrence of this exception. Class does not exist exceptions may occur when these classes are loaded implicitly.
  • ClassCastException: this error occurred while casting   

  Automatic checks are performed when the JVM performs type conversions  

  1.   Ordinary object: must be an instance object or subclass object of the target class; if it is an interface, the object is a subclass object of the interface
  2.       Array object: The target class must be an array type or Object under the java.lang package, Clonable, Serializable under the java.io package

  The method to solve the type conversion exception: explicitly specify the object type; judge whether it is the target object type through instanceof, and then perform the type conversion.

Six: JVM architecture and working methods

  • JVM is an architecture that simulates a real computer to achieve the computing functions of a real computer. computer-centric architecture of a real computer
    •   Instruction set: A set of all commands in machine language that a computer can recognize
    •        Computing unit: a functional module that can recognize and control the execution of instructions
    •        Register: The core component of the central processing unit, used for temporary storage, instructions, addresses and data
    •        Storage unit: A unit capable of storing computer operands and operating structures, such as memory and disk
    •        Addressing mode: range of addresses, minimum and maximum address ranges, and operating rules for addresses

                  

  •   Architecture of JVM:
    •    Execution engine: Equivalent to CPU, control instruction execution. Parse the bytecode file and get the parsing result.
    •         pc register: A pc register is created when each thread starts. The register holds the address of the currently executing JVM instruction. The register that holds the address of the next instruction to be executed is: the pc register. It always holds the address of the next instruction to be executed. The address can be a local pointer or an address within the method relative to the method's start instruction.
    •         Native method stack: the area where native methods are saved
    •         Heap: Holds the created object instance. All class objects are created through new, and a reference to the object is created on the stack after the object is created.
    •         Method area: also known as static area: save method data, classes, static variables, static methods, constants and member methods
    •         Runtime constant pool: Stores the constants of classes and interfaces, in addition to the references to member methods and member variables. The JVM runtime uses these references to find the actual address

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325499393&siteId=291194637