This is an article written earlier.ArticleRecently, I saw a discussion about classloader in J-channel, so I flipped it out again.
Static library and dynamic Connection Library
ProgramCompilation usually requires several steps: editing, compilation, connection, loading, and running. In our applications, there are some commonCodeTo use the code repeatedly, compile the code into a "library" file. In the connection step, the connector obtains the required code from the library file and copies it to the generated executable file. This type of library is called a static library. It features an executable file that contains a complete copy of the library code. The disadvantage is that multiple redundant copies are generated when used for multiple times.
To overcome this disadvantage, you can use a dynamic Connection Library. In this case, the connector is only marked in the executable file to indicate which dynamic connection libraries need to be used. When the program is running, the loader loads the required dynamic Connection Library to the memory based on these flags.
In addition, in the current programming environment, methods are generally provided for the program to load and run a specific dynamic connection library during running, you can also uninstall it (for example, the loadlibrary () & freelibrary () of Win32 and the dlopen () & dlclose () of POSIX ()). This function is widely used to update the appearance of some functional modules or programs when the program is running.
What is classloader?
Unlike normal programs, Java programs (class files) are not local executable programs. When running a Java program, first run the JVM (Java Virtual Machine) and then load the Java class into the JVM for running. This part is called class loader.
JVM itself contains a classloader called Bootstrap classloader. Like JVM, bootstrap classloader is implemented using local code and is responsible for loading core Java classes (classes starting with all java ). In addition, the JVM also provides two classloader, which are all written in Java and loaded by bootstrap classloader. The extension classloader is responsible for loading the extended Java class (for example, all javax. * classes starting with JRE and classes stored in the ext directory of JRE). Application classloader is responsible for loading the classes of the application.
When en to load the class?
When Will JVM use classloader to load a class? When you use Java to execute a class, JVM uses Application classloader to load this class. If Class A references Class B, whether it is directly referenced or used class. forname () reference, JVM will find the classloader that loads Class A, and use this classloader to load Class B.
Why use your own classloader?
It seems that the JVM's classloader is enough. Why do we need to create our own classloader?
Because the built-in classloader of JVM only knows how to load standard Java class files from the local file system. If you write your own classloader, you can:
1) automatically verifies the digital signature before executing the untrusted code
2) dynamically create custom build classes that meet specific user needs
3) Obtain a Java class from a specific place, such as a database
4) and so on
In fact, when using an applet, a specific classloader is used, because Java class needs to be loaded from the network and related security information needs to be checked.
Currently, most application servers use the classloader technology. Even if you do not need to create your own classloader, understanding its principles can help you better deploy your own applications.
Classloader Tree & Delegation Model
When you decide to create your own classloader, You Need To Inherit java. Lang. classloader or its subclass. When instantiating each classloader object, you must specify a parent object. If no parent object is specified, the system automatically specifies classloader. getsystemclassloader () as the parent object. For example:
After Java 1.2, Java class is loaded using the so-called delegate mode (delegation modle). When calling a classloader. loadclass () to load a class, follow these steps:
1) check whether this class has been loaded in?
2) If the class has not been loaded, call the parent object to load the class.
3) if the parent object cannot be loaded, call findclass () of the object to obtain the class.
Therefore, when creating your own class loader, you only need to overload the findclass () method.
Unloading? Reloading?
After a Java class is loaded to JVM, is it likely to be uninstalled? We know that Win32 has freelibrary () function, and POSIX has dlclose () function which can be called to uninstall the specified dynamic connection library, but Java does not provide an unloadclass () to uninstall the specified class.
In Java, the uninstallation of Java class is only an optimization of the system, which helps reduce the memory usage of applications. Since it is an optimization method, the JVM decides how to implement it, which is completely transparent to Java developers.
When will a Java class/interface be uninstalled? Sun said the same thing: "Class or interface may be unloaded if and only if its class loader is unreachable. Classes loaded by the bootstrap loader may not be unloaded ."
In fact, we are not concerned about how to uninstall the class. We are concerned about how to update the loaded class to update the function of the application. JSP is a very typical example. If a JSP file is changed, the application server needs to re-compile the modified JSP file, then load the new class to respond to subsequent requests.
In fact, a loaded class cannot be updated. If you try to use the same classloader to load the same class again, you will get an exception (Java. lang. linkageerror: duplicate class definition), we can only re-create a new classloader instance to re-load the new class. As for the previously loaded class, developers do not have to worry about it, because it may also have instances being used, as long as the related instances are all recycled by memory, then, the JVM will unmount the class that will not be used again when appropriate.
References and image source-Understanding J2EE application server class loading ubuntures
Http://www.blogbus.com/blogbus/blog/diary.php? Diaryid = 153255