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.

Clone this wiki locally