Wednesday, January 5, 2022

JVM Architecture Part - 1

 JVM Architecture Part - 1

I will be explaining the architecture of JVM. That is the Java Virtual Machine Architecture.

Java Application contains many java classes.

The main function of JVM is to load and execute the java application.

When you have your java class written in MyClass.java

We use javac MyClass.java command to compile this class.

This compilation creates a .class file that has the bytecode.

So compilation is process of converting java source code into byte code.

Once the bytecode is generated and is present .class file,

We invoke, java MyClass command to execute the program

When we invoke this java command, that is when the JVM comes into picture, a JVM instance will be created to load and execute the given class.

JVM has 3 different subsystems

1.      Class Loader Subsystem

2.      Runtime Data Areas

3.      Execution Engine



 

Now we will see how these subsystems work together to accomplish the given execution task.

1.    Class Loader Subsystem:

 



The Class Loader Subsystem has 3 phases

1.     Load

§  Load phase does the loading the bytecode that is your .class file into memory.

§  It can load byte code from various sources

§  Like in our example, When you call java MyClass, the class loader loads the byte code      from the file system.

§  Like this, it can also load bytecode from network socket.

Load phase involves 3 different class loaders

                                                    i.     Bootstrap Class Loader

Thiis is responsible for loading the internal classes those are distributed with your jvm implementation while you installed java.

                                                  ii.     Extension Class Loader

This is responsible for loading whatever class files that are present in          jre/lib/ext.

                                                 iii.     Application Class Loader

This is responsible for loading classes specified in the CLASSPATH environment variable.

If you specify –cp parameter along with java command, then application class loader will also loads the classes specified in –cp parameter as well.

 

2.     Link

Link phase has 3 different phases

                                                    i.     Verify

Verify phase is responsible for checking

·       whether the byte code is valid java byte code or not

·       Whether it is in proper format that will be interpreted or not.

                                                  ii.     Prepare

In this step, memory is allocated for the class level static variables.

When we declare any static variable in a class like this

Private static boolean isEligible=true;

Memory will be allocated for this static variable in this phase.

Since its memory allocation, some default vlaue will be assigned to it. In this case its false will be assigned to this boolean variable.

                                                 iii.     Resolve

All symbolic references inside the current class are resolved in this phase.

Suppose if the current class is having references to other classes or any constant pools, those references will be substituted by the actual references in this phase.

Though these 3 phases are depicted in sequential manner, they may execute in parallel and sometimes the phases may overlap based on the JVM implementation.

3.     Initialize

This phase is responsible for executing your static initializers.

Say for example, your class that is having some static block like

Static{

}

This will be executed in this phase.

Also,

If your class is assigned with any values to static variables, those values are set into memory in this phase. This will override the default values that were set to the same static variables in prepare step of Link phase.

 

That is about Class loader sub system

There are exceptions that we generally see while executing java program

ClassNotFoundException

This will be thrown when the class loader is unable to load the class from any of the sources in the load phase.

ClassDefNotFoundException

Suppose if there are two classes A and B and while we are running the class A,

A will be loaded properly by class loader.

Then in Resolve step of Link phase, as the class A is having reference to B and if it is unable to find class B, then it will through the ClassDefNotFoundException.


Run Time Data Areas concept is explained here.

No comments:

Post a Comment

Difference between Procedure Oriented Programming System and Object Oriented Programming System (POPS vs OOPS)

POPS vs OOPS   Today we will get to know the difference between Procedure oriented Programming system (POPS) and  Object-Oriented  Programmi...