X

Play AlgoQuiz

Rate Us :



Share with Friends :

Menu

JVM Architecture Specification :


Virtual Machine :-

It is the software simulation for a machine which can perform operations like a physical machine.


Types of Virtual Machines :

There are two types of Virtual Machines :-

  • Hardware Based or System Based Virtual Machines




  • Software Based or Application Based or Process Based Virtual Machines




Hardware Based or System Based Virtual Machines :

It provides several logical systems on the same computer with strong isolation from each other.

Examples :-

  • KVM (Kernel-Based Virtual Machine) for Linux Systems




  • VMware (Virtual Machine ware)




  • Xen




The main advantage of Hardware Based Virtual Machine is for effective utilisation of hardware resources.


Software Based or Application Based or Process Based Virtual Machines :

It acts as Runtime Engine to run a particular programming language applications.

Examples :-

  • JVM (Java Virtual Machine) acts as Runtime Engine to run Java applications.




  • PVM (Parrot VM) acts as Runtime Engine to run scripting languages like PERL.




  • CLR (Common Language Runtime) acts as Runtime Engine to run .Net based applications.




JVM (Java Virtual Machine) :

JVM is the part of JRE (Java Runtime Environment) which is a part of JDK (Java Development Kit).

JVM is responsible to load and run Java applications.

JVM is platform dependent program.

JVM is a software developed by C & C++. It is a virtual machine because it doesn't have any hardware i.e. fully build of software.


ClassLoader Sub System :

ClassLoader Sub System is responsible for the following three activities :-

  • Loading




  • Linking




  • Initialization




Loading :-

Loading means reading class files and store corresponding binary data in method area of JVM.

For each class file JVM will store the following information in method area :-

  • Fully qualified name of the Loader class or interface or enum.

  • Fully qualified name of its intermediate Parent class.

  • Whether .class file is related to class or interface or enum.

  • The modifiers information.

  • Variables or Field information.

  • Methods information.

  • Constant Pool information and so on.

After loading .class file in method area immediately JVM will create an object of the type class Class (present in java.lang package) to represent class level binary information on the heap memory.

The class object can be used by programmer to get class level information like fully qualified name of the class, Parent name, methods and variables information etc.

Example1 :

import java.lang.reflect.*; class Algo { private String s; private int i; public String getString() { return s; } public void setNum(int i) { this.i = i; } } class Example { public static void main(String[ ] args) throws Exception { Class c = Class.forName("Algo"); Method[ ] m = c.getDeclaredMethods(); for(Method m1 : m) { System.out.println(m1); } Field[ ] f = c.getDeclaredFields(); for(Field f1 : f) { System.out.println(f1); } } }

Output :

public java.lang.String Algo.getString() public void Algo.setNum(int) private java.lang.String Algo.s private int Algo.i

Note :

.class will be loaded once even though we create multiple objects.

For every loaded .class file only one class Class object will be created by JVM even though we are using that class multiple times in our program.

Example2 :

class Algo { private String s; private int i; public String getString() { return s; } public void setNum(int i) { this.i = i; } } class Example { public static void main(String[ ] args) throws Exception { Algo obj1 = new Algo(); Class c1 = obj1.getClass(); Algo obj2 = new Algo(); Class c2 = obj2.getClass(); System.out.println(c1.hashCode()); System.out.println(c2.hashCode()); System.out.println(c1 == c2); System.out.println(c1.equals(c2)); } }

Output :

1977939562 1977939562 true true Note : Hashcode may differ for the different execution of program but for both classes c1 and c2 it will be same.

Linking :

Linking consists of three activities :-

  • Verification




  • Preparation




  • Resolution




Verification :-

It is the process of ensuring that binary representation of the class structure is correct or not.

JVM will check whether .class file generated by valid compiler or not i.e. whether .class file is properly formatted or not.

Internally bytecode verifier which is a part of ClassLoader Sub System is responsible for this activity.

If verification fails then we will get runtime exception saying java.lang.VerifyError.


Preparation :-

In this phase JVM will allocate memory for the class level static variables and assign default values (but not original values) i.e. original values will be assigned in Initialization phase.


Resolution :-

It is the process of replacing symbolic references used in our class with original direct references from method area.

Symbolic references are resolved into direct references by searching through method area to locate the referenced entity.

class Algo
{
 public static void main(String[ ] args)
   {
     String s1 = new String("Valley");
	 Student s2 = new Student();
   }   
} 
 

For the above class, ClassLoader Sub System loads Algo.class , String.class , Student.class and Object.class.

The names of these classes are stored in Constant Pool of class Algo.

In Resolution phase these names are replaced with actual reference from method area.


Initialization :-

In this phase all static variables will be assigned with original values and static block will be executed from the top to bottom and from Parent to Child class.


Note :

While Loading, Linking and Initialization if any error occurs then we will get runtime exception saying java.lang.LinkageError. Of course VerifyError is a child class of LinkageError.


Types of Class Loaders :

Every Class Loader Sub System contains three Class Loaders :-

  • Bootstrap Class Loader / Primordial Class Loader




  • Extension Class Loader




  • Application Class Loader / System Class Loader




Bootstrap Class Loader / Primordial Class Loader :-

This Class Loader is responsible for loading Core Java API classes i.e. the classes present in rt.jar.

Location : jdk\jre\lib\rt.jar

This location is called Bootstrap Class Path i.e. Bootstrap Class Loader is responsible to load classes from Bootstrap Class Path.

Bootstrap Class Loader is by default available with JVM. It is implemented in Native languages like C and C++.


Extension Class Loader :-

It is the child of Bootstrap Class Loader. The Class Loader is responsible to load classes from Extension Class Path.

Location : jdk\jre\lib\ext\*.jar

This Class Loader is implemented in Java and the corresponding .class file name is sun.misc.Launcher$ExtClassLoader.class


Application Class Loader / System Class Loader :-

It is a child of Extension Class Loader. This Class Loader is responsible to load classes from Application Class Path. It internally uses Environment Variable Class Path. Application Class Loader is implemented in Java and the corresponding .class file name is sun.misc.Launcher$AppClassLoader.class

class Algo
{
 public static void main(String[ ] args)
   {
     System.out.println(String.class.getClassLoader());
     System.out.println(Student.class.getClassLoader());
     System.out.println(Algo.class.getClassLoader());
   }   
} 

Assume that Student.class present in both Extension and Application Class Path and Algo.class present in only Application Class Path.

For String class :-

From Bootstrap Class Path by Bootstrap Class Loader output is null because Bootstrap Class Loader is not implemented in Java and it is not a Java object.

For Student class :-

From Extension Class Path by Extension Class Loader output is sun.misc.Launcher$ExtClassLoader@35ce36

For Algo.class :-

From Application Class Path by Application Class Loader output is sun.misc.Launcher$AppClassLoader@11b86e7


Note :

Bootstrap Class Loader is not Java object. Hence we are getting null in the first case but Extension Class Loader and Application Class Loader are Java object and hence we are getting proper output ClassName@HexaDecimal_String_of_Hashcode

Class Loader Sub System will give highest priority for Bootstrap Class Path and then Extension Class Path followed by Application Class Path.


Working principle of Class Loader :


Class Loader follows Delegation Hierarchy Principle.

Whenever JVM comes across a particular class, first it will check whether the corresponding classes already loaded or not.

If it is already loaded in method area then JVM will use that loaded class.

If it is not already loaded then JVM request Class Loader Sub System to load that particular class, then Class Loader Sub System handover the request to Application Class Loader.

Application Class Loader delegates request to Extension Class Loader and Extension Class Loader in turn delegates to Bootstrap Class Loader.

Bootstrap Class Loader searches in Bootstrap Class Path (jdk\jre\lib). If the specified class is available then it will be loaded otherwise Bootstrap Class Loader delegates the request to Extension Class Loader.

Extension Class Loader will searches in Extension Class Path (jdk\jre\lib\ext). If the specified class is available then it will be loaded. Otherwise it delegates the request to Application Class.

Application Class Loader will search in Application Class path. If the specified class is already available then it will be loaded otherwise we will get runtime exception saying ClassNotFoundException.


Customized Class Loader :

Sometimes we may not satisfied with default Class Loaders and with default Class Loading mechanism. Default Class Loader will load .class file only once even though we are using multiple times that class in our program. After loading .class file if it is modified outside, then default Class Loader won't load updated version of class file because .class file is already there in a method area.

We can resolve this problem by defining our own Customized Class Loader. For example, whenever we are using a class, first my class loader will check whether updated version is available or not. If it is available then load updated version otherwise use already loaded existing .class file, so that updated version available to our program.

The main advantage of Customized Class Loader is we can Customize Class Loading mechanism based on our requirement.

We can define our own Customized Class Loader by extending java.lang.ClassLoader class.

Pseudo Code :

public class CustomClassLoader extends ClassLoader
{
  public Class loadClass(String cname) throws ClassNotFoundException
  {
    Check whether updated version is available or not. If updated version is available then
    load updated version and returns the corresponding class Class object otherwise return 
    class Class object of already loaded .class
  }
}

class CustomClassLoaderTest
{
 public static void main(String[ ] args) throws Exception
  {
     Algo obj = new Algo(); //Default class loader loads .class file
     CustomClassLoader c = new CustomClassLoader();
     c.loadClass("Algo"); /*Customized Class Loader checks for updates of updated version available
	                    then load updated version otherwise use already loaded .class*/

     c.loadClass("Algo"); /*Customized Class Loader checks for updates of updated version available
	                    then load updated version otherwise use already loaded .class*/
  }
}

Customized Class Loader is applicable for developing Web Servers and Application Servers.

We can use Class Loader to define our own Customized Class Loader. Every Customized Class Loader in Java should extends java.lang.ClassLoader class either directly or indirectly. Hence, this class acts as base class for developing our own Customized Class Loaders.


Memory Area :

There are five memory area in JVM :-

  • Method Area




  • Heap Area




  • Stack Area




  • PC Register




  • Native Method Stack




Method Area :-

For every JVM one method area will be created which is a shared area.

In method area class level data and static variables are stored.


Heap Area :-

For every JVM one heap area will be created which is a shared area.

Object data, corresponding instance variables and Arrays will be stored in heap area.


Stack Area :-

For every thread a separate runtime stack area will be created.

For every method call one entry will be stored in the sack that entry in the stack is called stack frame.

Every stack frame contain three parts : Local Variable Array (local variables and corresponding values will be saved in local variable array), Frame Data (it contains all symbols used in the method) and Operand Stack (if any intermediate operation is required to perform then the operand stack acts as the runtime workspace).

Each and every method call performed by the thread including local variables are stored in stack area.

The data which is stored in the stack area is always thread safe.


PC Register :-

For every thread a separate PC register will be created.

It holds address of current executing instruction. Once current executing instruction completed automatically PC register will be updated to next instruction.


Native Method Stack :-

For every thread a separate runtime stack will be created to hold native method information.


Execution Engine :

It is responsible to run the loaded .class file line by line.

It is the central component of JVM.


Execution Engine mainly has two parts :-

  • Interpreter




  • JIT Compiler




Interpreter :-

Interpreter is responsible to read, interpret and execute Java program line by line.


JIT Compiler :-

If any method repeatedly required instead of interpreting every time, JIT compiler is responsible for just compile the repeated method once into machine code and from next time we can use directly that machine code.

JIT compiler concept applicable only for repeatedly required method not for every method.

Total Java program must be interpreted once.

JIT compiler acts when there is repeatedly required method or hotspot method which is take care by special component called profiler.

Various components of JIT Compiler :

  • Intermediate Code Generator




  • Code Optimizer




  • Target Code Generator




Intermediate Code Generator :-

It produces intermediate code.


Code Optimizer :-

It is responsible to optimize the code.


Target Code Generator :-

It is responsible to generate machine code or native code.


There are several other components in Execution Engine like Garbage Collector, Security Manager etc.

While executing execution engine sometime we may require the native method libraries information which is provided by Java Native Interface (JNI).



X

MiniDoll :

MiniDoll is an application to learn with fun. You can play it to solve some tricky questions related to Java Programming. Either enjoy playing it alone or discuss with your friends to solve it. It is a simple approach to learn by solving questions. This application has different topics of Java and each topic is divided into three phases. Start solving from initial topic to final topic and by the end of every topic you can boost your concept of that topic.