Class loaders in Java

Bootstrap ClassLoader

Bootstrap ClassLoader: starts the class loading. By default, it loads many jar classes in the JDK \lib directory. This path can be specified by the -xbootCLASspath parameter

Extension ClassLoader

Jar classes in the JDK /lib/ext/ directory are loaded by default. This path can be specified using the java.ext.dirs system variable

Application ClassLoader

Application class loader, responsible for loading many classes written by the developer

User ClassLoader (custom ClassLoader)

Custom class loaders solve special cases that the above loaders cannot solve

2. Parental delegation model

In our system, when the JVM loads a class, it gives the loading task to the Application ClassLoader, and the Application ClassLoader tries to give the loading task to the Extension ClassLoad. However, the Extension ClassLoader will assign the task to the Bootstrap ClassLoader. Boostrap ClassLoad will search/JDK /lib to see if it exists. If it does not exist, it will push the loading task down until the loading is complete.

JVM class loading process

Java class loading is divided into five processes: load, link (verify, prepare, parse), initialize, use, and unload.

Loading mainly involves reading a. Class file through a binary byte stream into the JVM. In the reload phase, the following three tasks need to be done: fetch the corresponding class file from the classpath using the ClassLoader and read it into memory as a binary stream. Converting the static storage structure represented by the byte stream to the runtime data structure of the method area generates a java.lang.calss object of this class in memory as an access point link to the method area class. 2.1 validation mainly ensures that the loaded byte stream complies with the JVM’s specifications. The verification phase mainly completes the following four working file format verification metadata verification (whether it conforms to the Java language specification) bytecode verification (to ensure the semantics of the program is legal and logical) symbol reference verification (to ensure the normal execution of the next parsing) 2.2 Preparation Mainly allocate memory for static variables in the method. In the initialization phase, the last step in the class loading process, mainly refers to the variable assignment of the active taste buds according to the assignment statement in the program. When there is an inheritance relationship, the parent class is initialized before the child class is initialized, so when creating a subclass there are actually two instances of objects in memory using program calls to unload the JVM garbage collector collector to complete the collection