java class loading mechanism, class loader, custom class loader

Class loading mechanism

java class from being loaded into the JVM to unload the JVM, entire life cycle comprising: loading (Loading), verification (Verification), preparation (Preparation), parsing (Resolution), initializing (Initialization), the use of (using), and unloading ( Unloading) seven stages. Wherein the verification, preparation and analytical three portions collectively connected (Linking).

 

1, Load

Loading refers to the binary data read class .class files into memory, the method in which region the runtime data area, and then create a heap java.lang.Class object, used to package the class in the method of the data structure area.

 

Class loaded by the JVM class loader provides complete, the class loader is the base run. All class files when the program started, and not to use the one-time loader, but if necessary, by java class loader mechanism (classLoader) to dynamically load a class file into memory.

jvm runtime will produce three classLoader:

Start class loader (ClassLoader BootStrap) : java class loader hierarchy is the top-most class loader is responsible for loading the core jdk libraries. Implemented by C ++, not a subclass of classLoader.

Extension class loader (ClassLoader the Extension) : responsible for loading the java extension libraries, such lib / ext java.ext.dirs system property or the directory specified by a jar. Parent class loader to null.

System class loader (the App ClassLoader) : responsible for loading the -classpath options from the java command, java.class.path system attribute specified class path and a jar. Program () can be acquired by the system class loader classLoader static method of getSystemClassLoader. Implemented by the java language, the parent class loader is ExtClassLoader.

In addition to these three classLoader java provided by default, users can define their own classLoader needed, these custom classLoader must inherit from java.lang.ClassLoader class.

By using a different class loader, the binary data from the different sources can be loaded classes. Usually following situations:

Load class files from the local file system, which is the way most of the class loader instance of the program. Jar package loaded class from class, this approach also common. Loaded through a network to a class java class file dynamically compiled source, and perform a load, such as jsp.

2, the connection

When after the class is loaded, the system generates a corresponding class whom the object, and then into the connecting stage (validation - Preparation - Analytical) connected to the merge phase is responsible for the binary data to the class in jre.

Verify: means for detecting whether the loaded class has the correct internal structures, and other types and coordinated. Four authentication comprising: verification file format, metadata validation, verification byte and verification of symbolic references. Preparation: responsible for allocating memory for class variables, and set the default initial value. Analysis: The binary data variable in the class replaced by a direct reference symbol reference.

3, initialization

In the initialization phase, the class static variable mainly given the correct initial value. In fact, the implementation class constructor <clinit> () method. Java class specified in the initial value of the class variable in two ways: a class declaration specifies the initial value when the variable; B block using the static initialization for the class specified variable initial value.

jvm initialize a class comprising the steps of:

Loading and initializing its direct connection to the class parent class initialization statements executed sequentially when the step 2, the initialization of the system follows the direct parent of 1 to 3, and so on.

When a class is actively quoted triggers initialization process:

Encounter new, getstatic, putstatic invokestatic or 4 when this bytecode instruction, if the class is not been initialized, initialization is required to trigger it. 4 which generates Java code instruction the most common scenario is: when using the new keyword to instantiate objects, to read or set a static field of a class (the final modification, the result has been placed in the constant pool except for the compiler static field time), as well as calling a static method of class time. When using java.lang.reflect package approach to reflect the class called, if the class is not been initialized, you need to trigger its initialization. When initializing a class, if you find the parent class has not been initialized, you need to trigger initialization of the parent class. When the virtual machine starts, the user needs to specify a main class execution (including main () class method), the virtual machine to initialize this class. When using a dynamic language support jdk7 +, java.lang.invoke.MethodHandle instance if the final analysis result REF_getStatic, REF_putStatic, REF_invokeStatic the method handle, and this method handle class corresponding not been initialized, initialization is required to trigger . When a class if it is passive quoted, does not trigger initialization process:

Reference a static field of the parent class by sub-category, sub-category does not cause initialization. For static fields, only directly define the field of class will be initialized, so when we refer to a static field in the parent class defined by the subclass only trigger initialize the parent class, without triggering initialize the subclass. To refer to the class by the array definition, it does not trigger this type of initialization. Constants at compile time will be credited to the constant pool class is called, there is no direct reference to the definition of the class constants of nature, it will not trigger initialization class defined constants.

4, using

(slightly)

5. Uninstall

If the following happens, classes will be uninstalled:

All instances of the class have been recovered, that is java heap any instance of the class does not exist. Load class ClassLoader has been recovered. Java.lang.Class corresponding to the class object is not referenced anywhere, not reflected by the method of accessing the class anywhere. If the above three conditions are satisfied, jvm class will be unloaded when garbage collection methods district, the uninstall process is actually kind of cleared area in the method class information, java classes throughout the life cycle is over.

 

 

Class loader

Class loader is responsible for loading all classes. Which generates a java.lang.Class instance object is loaded into memory for all classes. Once a class is loaded as the JVM, the same class will not be loaded again. Just as an object has a unique identifier, like a load JVM class also has a unique identifier. In Java, a class by its fully qualified class name (including the package name and class name) as an identifier; however, in the JVM, a class with its fully qualified class name and its class loader as its unique identification. For example, if there is a class called Person in pg package, by the class loader is responsible for loading ClassLoader Examples kl, corresponding to the Person class Class object is shown in the JVM (Person.pg.kl). This means that two of the same name class loader loads the class: (Person.pg.kl) and (Person.pg.kl2) are different, they are loaded class is completely different, incompatible.

We have already introduced several java class loader in, let's use a diagram to show their level:

 

Class loading step

Class loader loads class 8 generally requires the following steps:

Detecting whether the load over this Class, i.e. whether there is in this Class buffer, if there is go directly to Step 8, otherwise go to step 2. If no parent class loader, then either Parent is the root class loader, or is itself the root class loader, skip to step 4, if the parent class loader is present, go to step 3. Request using the parent class loader to load a target class, if successful loading skip to step 8, otherwise continue to step 5. Request the root class loader to load a target class, if successful loading skip to step 8, otherwise skip to step 7. The current class loader tries to find Class file, if found then step 6, if you can not find the step 7. Class load from the file, after a successful skip to step 8. ClassNotFountException throw an exception. Java.lang.Class returns the corresponding object.

Class loading mechanism

Overall responsibility: When a class loader is responsible for loading a Class, the Class depends and will also be references to other Class This class loader is responsible for loading, unless it appears from the use of another class loader to load. Delegate parents: let the parent class loader tries to load the Class, only in the parent class loader when the class tries to load the class can not be loaded from your own class path. Popular speaking, is a particular class loader to load a class, upon request, the first task will be entrusted to the parent loader loads, in turn recursion, if the parent class loader can load complete the task successfully return; only the Father when the loader can not complete this task load, load it themselves. Cache Mechanism: to ensure that all loads over the Class will be cached, when the program requires the use of a Class, class loader first from the buffer zone in search of the Class, the Class object only when there is no buffer zone, the system will reading corresponding to such binary data, and converts it into Class object, stored in the buffer. This is after it is modified the Class, you must restart the reasons JVM, the program edits will not force.

Custom class loader

All jvm class loader except with class loader are instances of subclasses of ClassLoader, developers can implement a custom class loader subclass ClassLoader by expanding and method ClassLoader contains rewritten. ClassLoader following two key ways:

loadClass (String name, boolean resolve): This method is ClassLoader entry point to load the class with the specified name, the system calls the method is to acquire ClassLoader class object for the specified class. findClass (String name): If you need to find the class to implement custom ClassLoader with the specified name, you can do so by rewriting the above two methods, is generally recommended to rewrite findClass () method instead of loadClass () method. Step classLoader () method: 1) findLoadedClass (): to check whether the class is loaded, if the load is directly returned; 2) call loadClass () method on the parent class loader. If the parent class loader is null, with the use of the class loader loads; 3) call findClass (String) method to find the class. As can be seen from this, override the findClass () method avoids override the default class loader delegate parent class, two strategies buffering mechanism; if override loadClass () method, the implementation logic is more complex.

Published 230 original articles · won praise 246 · views 310 000 +

Guess you like

Origin blog.csdn.net/kangbin825/article/details/105335633