Friday, June 12, 2020

Java Class Loader



The Class loader is part of the JVM that dynamically loads Java classes into the Java Virtual Machine. The Java run time system does not need to know about the files and the file systems because of class loaders.Java classes aren’t loaded into memory all at once, but as and when required by an application. At this point, the Java Class loader is called by the JRE and these Class loaders load classes into memory dynamically.

Class loaders are hierarchical. Classes are introduced into the JVM as they are referenced by name in a class that is already running in the JVM. So how is the very first class loadded?  The very first class is specially loaded with the help of static main() method declared in the class.All the subsequently loaded classes are loaded by the classes, which are already loaded and running.

A class loader creates a namespace.All JVMs include at least one class loader that is embedded with in the JVM called the primordial ( or Bootstrap) class loader. Now lets look at non-primordial class loaders. The JVM has hooks in it to allow user defined class loaders to be used in place of primordial class loader. Let us look at the class loaders created by the JVM.



CLASS LOADERRe-loadable?Description
Bootstrap (primordial)NoLoads JDK internal class. java.* packages (as discribed in the sun.boot.class.path system property, typically loads rt.jar and i18n.jar)
ExtensionsNoLoads jar files from JDK extensions directory (as defind in the java.ext.dirs system property - usally lib/ext directory of the JRE)
SystemNoLoads classes from system classpath (as defined by the java.class.path property,which is set by the CLASSPATH environment variable or -classpah or -cp command line options)





Class loaders are hierarchical and use a delegation model when loading a class. Class loaders request their parent to load the class first before attempting to load it themselves. When a class loader loads a class, the child class loader in the hierarchy will never reload the class again. 

Hence uniqueness is maintained. Classes loaded by a child class loader have visibility into classes loaded by its parents up the hierarchy but the reverse is not true as explained in the above diagram.

According to the uniqueness principle, a class loaded by the parent should not be loaded by Child Class Loader again. So, it is possible to write class loader which violates delegation and uniqueness principles and loads class by itself.
In short, class loader follows the following rule:

  • It checks if the class is already loaded.
  • If the class is not loaded, ask parent class loader to load the class.
  • If parent class loader cannot load class, attempt to load it in this class loader.
Delegation Model: The JVM and the Java Class loader use an algorithm called the Delegation Hierarchy Model Algorithm to load the classes into the Java file.
The Class loader works based on a set of operations given by the delegation model. 

  • Class loader always follows the Delegation Hierarchy Model Principle.
  • Whenever JVM comes across a class, it checks whether that class is already loaded or not.
  • If the Class is already loaded in the method area then the JVM proceeds with execution.
  • If the class is not present in the method area then the JVM asks the Java Class loader Sub-System to load that particular class, then Class loader sub-system hands over the control to Application Class loader.
  • Application Class loader then delegates the request to Extension Class loader and the Extension Class loader in turn delegates the request to Bootstrap Class loader.
  • Bootstrap Class loader will search in the Bootstrap classpath(JDK/JRE/LIB). If the class is available then it is loaded, if not the request is delegated to Extension Class loader.
  • Extension Class loader searches for the class in the Extension Classpath(JDK/JRE/LIB/EXT). If the class is available then it is loaded, if not the request is delegated to the Application Class loader.

Important Note : Two objects loaded by different class loaders are never equal even if they carry the same values. Which means a class is uniquely identified in the context of the associated class loader. This applies to Singleton classes too, where each class loader will have its own Singleton .

No comments:

Post a Comment

Hello Buddy, if you have any doubts or need any clarification , feel free to comment. Thanks.