Skip to content

JVM Architecture

Rohit Agarwal edited this page Jan 14, 2018 · 17 revisions

Virtual Machine

it is a software simulation of a machine which can perform operations like a physical machine. There are two type of virtual machines.

  1. Hardware based or system based - It provides several logical systems on the same computer with strong isolation from each other. That is on one physical machine we are defining multiple logical machines. The main advantage of hardware based virtual machines is hardware resources sharing and improve utilization of hardware resources.
    Example - KVM(Kernal based virtual machine for linux systems), VMWARE, XEN, Cloud computing

  2. Application based or process based - These virtual machines acts as runtime engines to run a particular programming language applications.
    Example -

    • JVM(Java virtual machine) acts as runtime engine to run java based applications.
    • PVM(Parallel virtual machine) acts as runtime engine to run perl based applications.
    • CLR(Common language runtime) acts as runtime engine to run .NET based applications.

Note - JVM is the part of JRE(part of JDK) and it is responsible to load and run java class files.

Basic Architecture diagram of JVM

Class loader subsystem is responsible for the following three activities.

  1. Loading
  2. Linking
  3. Initialization

Loading

Loading means reading class files and store corresponding binary data in method area. For each class file JVM will store corresponding information in the method area.

  • Fully qualified name of class
  • Fully qualified name of immediate parent class
  • Methods information
  • Variables information
  • Constructors information
  • Modifier information
  • Constant pool information etc

After loading .class file immediately JVM creates an object for that loaded class in the heap memory of type java.lang.Class.

The class Class object can be used by programmer to get class level information like methods information, variables information, constructor information etc.

Example

class Student {

	private int name;
	private int rollNumber;

	public int getName() {
		return name;
	}

	public void setName(int name) {
		this.name = name;
	}

	public int getRollNumber() {
		return rollNumber;
	}

	public void setRollNumber(int rollNumber) {
		this.rollNumber = rollNumber;
	}

}

public class Example1 {

	public static void main(String[] args) throws ClassNotFoundException {

		Class c = Student.class;
		Method[] m = c.getDeclaredMethods();
		for (Method m1 : m) {
			System.out.println(m1.getName());
		}
	}

}

For every loaded type only one class object will be created even though we are using the class multiple times in our program.

public class Example2 {

	public static void main(String[] args) {

		Student s1 = new Student();
		Class c1 = s1.getClass();
		Student s2 = new Student();
		Class c2 = s2.getClass();
		System.out.println(c1.hashCode());
		System.out.println(c2.hashCode());
		System.out.println(c1 == c2);// true

	}

}

In the above program even though we are using Student class multiple times only one Class class object got created.

Linking

It is responsible for three activities.

  1. Verify or Verification - It is the process of ensuring that binary representation of a class is structurally correct or not. That is JVM will check whether .class generated by valid compiler or not that is whether.class is properly formatted or not. Internally Bytecode verifier is responsible for this activity. Bytecode verifier is the part of class loader subsystem. If verification fails then we will get runtime exception saying java.lang.VerifyError.
  2. Prepare or Prepration - In this phase JVM will allocate memory for class level static variables and assign default values.
    Note - In the initialization phase original values will be assigned to the static variables and here only default values will be assigned.
  3. Resolve or Resolution - It is the process of replacing symbolic names in our program with original memory refrences from method area.

Example

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

For the above class class loader loads Test.class, String.class, Student.class and Object.class. The name of these classes are stored in constant pool of Test class. In resolution phase these names are replaced with original memory level references from method area.

Initialization

In this phase all static variables are assigned with original values and static blocks will be executed from parent to child and from top to bottom.

Note - While loading, linking and initialization if any error occurs then we will get runtime exception saying java.lang.LinkageError.

Types of Class loaders

  1. Bootstrap class loader or Primordial class loader
  2. Extension class loader
  3. Application class loader or System class loader

Bootstrap class loader or Primordial class loader

it is responsible to load core java Api classes ie the classes present in rt.jar.

This location is called bootstrap classpath ie bootstrap class loader is responsible to load classes from bootstrap classpath (JDK/JRE/lib).
Bootstrap class loader is by default available with every JVM. it is implemented in native languages like C/C++ and not implemented in java.

Extension class loader

It is the child class of bootstrap class loader. It is responsible to load classes from extension classpath (JDK/JRE/lib/ext). Extension class loader is implemented in java and corresponding .class file is sun.misc.Launcher$ExtClassLoader.class.

Application class loader

It is the child class of Extension class loader. This class loader is responsible to load classes from application classpath. It internally uses environment variable classpath. It is implemented in java and the corresponding .class file name is sun.misc.Launher$AppClassLoader.class.

Clone this wiki locally