Java (JVM) Class Loader


Class Loader

Class Loader is a component with the Java Execution Engine which loads the Binary data from the .class files available in the classpath into the Method Area . Loading of a class into the method area occurs only the first time when the class is referenced with in the running Java application. For all other references the data is reused from the method area, unless the class has been UNLOADED (Unloading of a class might occur at times when using a Custom Class Loader to Load a class, and that Custom Class Loader has been dereferenced and Garbage Collected. Under such conditions the classes loaded by that Custom Class Loader will be UNLOADED or Garbage Collected from the Method Area. Under such scenario if a Class is Unloaded, the next time it is referenced by the running Java Application it is Re-Loaded using a different Class Loader).



Class Loader Name Space Separation
Each Class Loader has its own name space, while loading each Class JVM keeps track of which Class Loader loaded the class. When a loaded class references another class, the JVM tries to load the class using the same Class Loader which loaded the referencing class. Hence by default classes loaded by a common Class Loader falls under the same Name Space and are visible to each other. Classes loaded by different Class Loaders fall into different Name Space and cannot gain access to each other. Hence one can enforce user defined security boundary across a given section of a Java application by using a Custom Class Loader to load classes’ specific to that section.

Let’s consider there are multiple Applets running in the same browser and each of them is loading code from a different source, but all of them are scheduled over the same Java Run Time or JVM. Under such condition none of the applets will have visibility of the other (unless explicitly done) as each of them will be loaded using a different Class Loader and hence will be on a different Name Space.

**Same JVM process can be shared between multiple applets, or applets may be placed into different processes depending on whether the existing JVMs match the applet requirements and have enough resources to execute the applet. But we can enforce usage of a SINGLE JRE/JVM by disabling the "next-generation" Java plugin in Java control panel


Parent Child Class Loader Delegation Model
Every Class Loader invoked with in the Java Execution Engine has a Parent Class Loader associated to it except for the ROOT Class Loader known as Boot-Strap Class Loader. While creating a custom Class Loader we can provide the Parent Class Loader to be associated with it or go with the one provided by the JVM. 

Responsibilities as per Class Loader hierarchy:
  • The Boot-Strap Class Loader is responsible for loading Java-API classes.
  • Application Class Loader is the default Class Loader for a running Java application, responsible for loading classes in the application classpath. Boot-Strap Class Loader is the Parent of Application Class Loader.
  • Custom Class Loader tries to load classes not with in the Host systems application’s classpath. (Example – Loading classes over the network from a Hosting Server).  Application Class Loader is the Parent of Custom Class Loader. Although Custom Class Loaders can also be the Parent of other Custom Class Loaders.


Delegation Model:

Every time a Class Loader is asked to load a class, it delegates the request to its Parent Class Loader. If it’s Parent Class Loader is not the Boot-Strap Class Loader the request is again delegated to its Grand Parent Class Loader. This mechanism occurs until the request reaches the Boot-Strap Class Loader. If the Boot-Strap Class Loader succeeds in loading the class, the class loading activity stops. But if it fails the request is again delegated to its immediate Child Class Loader, and this mechanism continues until it reaches the invoking Class Loader or if any of the intermediate Class Loaders are able to load the required class.

Whichever Class Loader is able to load the required class in the delegation model, the loaded class gets into that particular Class Loader’s Name Space.

Demonstrating the Parent Child Delegation model of the Class Loader using three Class Loading scenarios, each specific to a Class Loading hierarchy:




No comments:

Post a Comment