67

I am just now beginning to learn the internal architecture of Java. I have roughly understood the concept of class loading which loads the required classes when jvm runs, ClassNotFoundException is thrown when a class is not found and specific class loader loads the classes referenced by the class.

Can someone please explain clearly the flow of class loading i.e. the sequence of bootstrap class loading and user-defined class loading in the sample Java code below.

import java.io.File;
public class Sample
{
    public static void main(String[] args)
    {
        String fileName = "sample";
        File file = new File(fileName);
        file.isFile();
    }
} 

Also I learnt from a reference material that "classloader maintains the namespaces of the classes it loads". By namespaces, does that mean the literal names of the class? Also can someone please explain the implication/advantage of that?

Raedwald
  • 46,613
  • 43
  • 151
  • 237
Aarish Ramesh
  • 6,745
  • 15
  • 60
  • 105

5 Answers5

58

You will run your Sample class as follows

> java Sample

for little magic, check out the output of-verbose:class option and you see tons of following lines..

[Opened C:\jdk1.6.0_14\jre\lib\rt.jar]
[Loaded java.lang.Object from C:\jdk1.6.0_14\jre\lib\rt.jar]
[Loaded java.io.Serializable from C:\jdk1.6.0_14\jre\lib\rt.jar]
[Loaded java.lang.Comparable from C:\jdk1.6.0_14\jre\lib\rt.jar]
.
.
.
.
.
.
[Loaded java.security.cert.Certificate from C:\jdk1.6.0_14\jre\lib\rt.jar]
[Loaded Sample from file:/D:/tmp/]
[Loaded java.lang.Shutdown from C:\jdk1.6.0_14\jre\lib\rt.jar]
[Loaded java.lang.Shutdown$Lock from C:\jdk1.6.0_14\jre\lib\rt.jar]

You see a bunch of classes from \jre\lib\rt.jar are loaded, much before your class is loaded by Bootstrap class loader (or Primordial ). These are the pre-requisite for running any Java program hence loaded by Bootstrap.

Another set of jars is loaded by Extension class loader. In this particular example, there was no need of any classes from the lib \jre\lib\ext hence its not loaded. But Extension class loader are specifically assigned the task of loading the classes from the extension lib.

EDIT: Apart from the standard platform java classes Sun/Oracle also provide a set of jars which are used to extend the platform's core API. The jars placed in the extension lib folder are automatically placed in the classpath and hence not needed to be included in classpath explicitly. Here is nice official article on the same topic.

Finally, your class Sample is loaded by Application class loader after Bootstrap and Extension have finished loading.

Santosh
  • 17,667
  • 4
  • 54
  • 79
51

Classloader hierarchy

Whenever a new JVM is started the bootstrap classloader is responsible to load key Java classes (from java.lang package) and other runtime classes to the memory first. The bootstrap classloader is a parent of all other classloaders. Consequently, it is the only one without a parent.

Next comes the extension classloader. It has the bootstrap classloader as parent and is responsible for loading classes from all .jar files kept in the java.ext.dirs path–these are available regardless of the JVM’s classpath.

The third and most important classloader from a developer’s perspective is the system classpath classloader, which is an immediate child of the extension classloader. It loads classes from directories and jar files specified by the CLASSPATH environment variable, java.class.path system property or -classpath command line option.

Classloader hierarchy

ClassLoader Namespace

In Java a class is uniquely identified using ClassLoader + Class as the same class may be loaded by two different class loaders.

Class A loaded by ClassLoader A != Class A loaded by ClassLoader B

How is it helpful?

It is helpful for defining different protection and access policies for different classloaders. Take an example of applet which is loaded using a different classloader, you would not want a third party application all access to your resources. So for security its important to maintain different namespaces.

Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120
11

JVM maintains a runtime pool in permgen area where classes are loaded. Whenever a class is referenced default class loader finds the class in the class path and loads it into this pool. And this is not specific to user defined classes or classes provided in JDK. When a class is referenced it is loaded into the memory.

Classes loaded by the ClassLoader are stored internally in the ClassLoader instance

// The classes loaded by this class loader. The only purpose of this table
// is to keep the classes from being GC'ed until the loader is GC'ed.
private final Vector<Class<?>> classes = new Vector<>();

When class needs to be added to the memory following function is called:

// Invoked by the VM to record every loaded class with this loader.
void addClass(Class c) {
    classes.addElement(c);
}

Found a useful diagram on how Class Loaders work.

enter image description here

The Student Soul
  • 2,272
  • 2
  • 14
  • 12
Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
  • you mean to say that - " as long as its class loader exists, the class exists"?? – TheLostMind Dec 27 '13 at 06:50
  • 1
    Yes and ClassLoader instance does not get GCed as it is referenced by JVM thread. Infact that is why even if you have a Singleton class it is possible to create two instances with two different class loaders. – Aniket Thakur Dec 27 '13 at 06:53
  • So.. you mean to say "if I have several hundred classes being loaded by the same classloader, then until the class loader is GCed, the classes will not be GCed"??... How is this efficient? – TheLostMind Dec 27 '13 at 08:13
  • Well yes and it is actually JVM specific. The only way that a Class can be unloaded is if the Classloader used is garbage collected. Refer to http://stackoverflow.com/questions/148681/unloading-classes-in-java for more details. – Aniket Thakur Dec 27 '13 at 08:49
  • @TheLostMind Because classes are rarely referenced once and then never again, and class loading is not necessarily quick. Besides which, classes may be written under the assumption that they are only loaded once per program run, which would be violated if the classes are unloaded and then loaded again. (I don't know if the JLS guarantees this behavior but I've seen it in use.) – Chris Hayes Dec 27 '13 at 08:50
  • ok... thanks for the info :) .. one more thing... will GC treat class loaders in a different way while doing its operations??.. the above 2 answers imply that there will be no "generation-shift" of classes and all classes will be flagged as "in-use" so that GC doesnt happen on them.. – TheLostMind Dec 27 '13 at 08:56
  • 1
    No ClassLoader instances are same as any other Objects in the heap. The statement that it does not get GCed come from the fact that ClassLoaders have references from JVM threads which run till the java process is completed and JVM shuts down. For eg the BootStrap Class Loader is a native implementation meaning its code is embedded in JVM. So it's reference will always be alive. Hence we say they are not the potential candidates for GC. Other than that GC treats them the same way. – Aniket Thakur Dec 27 '13 at 09:12
  • 1
    @TheLostMind I can't say for sure, but the code Aniket posted in the answer implies that the GC treats them no differently; otherwise the classloader wouldn't need to keep a `Vector` around just to prevent garbage collection. – Chris Hayes Dec 27 '13 at 09:12
6

The Java Virtual Machine starts up by creating an initial class, which is specified in an implementation-dependent manner, using the bootstrap class loader (§5.3.1). The Java Virtual Machine then links the initial class, initializes it and the static instance variables declared in it and finally invokes the public class method void main(String[]). The invocation of this method drives all further execution. Execution of the Java Virtual Machine instructions constituting the main method may cause linking (and consequently creation) of additional classes and interfaces, as well as invocation of additional methods.

read this link

ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176
rachana
  • 3,344
  • 7
  • 30
  • 49
2

The loading process can be viewed as interaction between Classloader Subsystem and the Memory Area of JVM.

Classloader works in three general steps 1.) Loading 2.) Linking and 3.) Initialization.

The very basic interaction between Classloader Subsystem and the Memory Area happens during Linking (apart from other interactions!)

Linking activity is subdivided into i.) Verify ii.) Prepare and iii.) Resolve. Verify: is more for security, valid compilation is checked. In the step ii.) Prepare - static variable memory is allocated and assigned with default values. And in

iii.) Resolve: Symbolic references are replaced with original references from the "Method Area" which contains class level data and static variables.

JVM Arch

Nirmal
  • 1,229
  • 1
  • 15
  • 31