What is JVM (Java Virtual Machine) ?

Thamindu Sasitha
8 min readOct 5, 2021

--

As Java developers it is crucial to understand the fundamental concepts of Java and how the underlying architecture works.

Before moving into JVM, let’s understand the concept of Virtual Machines.

Virtual Machine

A machine that is not in the reality but it simulates the environment to make you feel real. In other words virtual machines don’t exist.

Eg : Virtual Class Room , Virtual Reality

Similarly you can’t download the JVM because it doesn’t exist as an entity. But you can install the JRE or JDK.

There are two types of virtual machines.

1. SVM ( System Based Virtual Machine )

This may have one or some hardware and this creates multiple environments you to work for multiple users or multiple instances.

Eg: Hypervisor, Xen

These environments are completely independent.

2. AVM (Application Based Virtual Machine)

These don’t have any hardware involved. But you may have an application/ software which helps you to create a platform to run other programs.

Eg : JVM — Java , CLR — .NET , PVM — Perl (Dynamic Language)

AVM doesn’t exist in reality. This is a process. Hence called as Process Based Virtual Machine. You cannot install or uninstall AVMs.

Java Virtual Machine (JVM)

JVM is completely a specification. It says how this should be done.

When you download JRE , JVM comes with it. When you install JRE it will deploy all the codes which can create a JVM. If you are installing JRE on a Windows Machine it will deploy the codes which require to create a JVM for Windows environment.

Is Java write once run anywhere ?

The program which is written to run on the server or different machine may not exactly run without modification on a mobile device.

It is a YES because it is just a limitation (reduced).

Even though Java is platform independent, JRE is tightly platform dependent.

How many JVM instances are there ?

When you execute a Java program it creates a JVM instance and it creates a JVM. JVM will take care of converting your byte code into machine code.

If you are not executing any program on your computer; then you don’t have a JVM instance on your computer.

The moment you start a Java program , it will create a JVM instance on your computer. The moment your program exits, the JVM instance also will die.

JVM exists while the applications running. JVM exists while at least non - daemon thread exists.

The moment you give ‘java <filename>’ in your terminal it means you are asking the Operating System to give you a java instance.

When JVM starts, it creates a non-daemon thread and your class must have a method called ‘public static void main’. It must be ‘void’ and ‘public’. When your JVM instance has created, it take this method to execute. This daemon thread is capable to create other non-daemon threads.

There is two ways this program gets exit (JVM gets destroyed).

1. If no non-daemon threads exist

All the non-daemon threads you created are closed/destroyed.

2.Application can do suicide

Calls the ‘System exit’ method.

Components of JVM

You have ‘.java’ files and you converted those into ‘.class’ files. Now ‘.class’ files are there.

You need to load those ‘.class’ files into JVM — 1. Class Loader

Now the loaded class should be stored in a memory area — 2. Memory Area

Now you have objects, instructions on your memory. Someone has to be there to execute 3. Execution Engine

JVM, JRE and JDK

1. Class Loader

Main responsibility of Class Loader is taking the class and loading it into the memory area.

Mainly we can find two type of class loaders in JVM.

  1. Bootstrap Class Loader
  2. Custom defined Class Loaders

Class Loader has 3 main responsibilities

1.Loading

The main responsibility is taking your class file and putting it into the memory.

It reads fully qualified class name (package.sample.Name) first. Also, it reads about variable information, immediate parent information(if no parent : the object would be the parent) and whether it is a class or an interface or enum. Then it load into the memory area.

Each and every class when it is loaded JVM does a special thing. It creates a object from class type(Not from your class type; There is a special type in Java called ‘class’).

Let’s assume you have ‘Employee’ class and in a ‘Leave’ class you create a object from the Employee ( Employee e = new Employee). To load this ‘Employee’ class to JVM, it reads those information ,creates object from class type (Data type), assigns this Employee into that class and puts it into the Heap. For the very first time only the JVM create object from the class type (Only one object per class would create). If there is an other class called ‘Manager’ and there also you create a object from the Employee. But this time JVM will not create object from the class type.

2. Linking

Linking is divided into 3 steps.

1.Verification

When it comes to the Java it is not easy to crack software because Java has a byte code verifier in JVM. There it checks whether the class is safe to execute or not. That’s why Java is safe to execute in any environment.

When you try to load Java class, there is a sub program called bytecode verifier. It will verify whether this come from valid compiler, whether this has correct structure and whether this class file has correct formatting. If any of these are not satisfied JVM throw run time exception called verify exception. If you ever see a verify exception that is a indication that your class file is altered somewhere.

If everything is done then verification process is complete. Then it moves to preparation process.

2.Preparation

If you use any instance level variable or static variable in your class the preparation part will assign a default value for that. This is a default value , but not the initial value.

Let’s assume you have :

Static int a = 10;

This process will assign 0 for that, but not the 10.

There is a default value for each variable (Boolean — False, Int — 0,…) . If it is a object it is a ‘null’. This is a programmer friendly work that JVM does.

3.Resolution

You can create your class called ‘Employee’ or anything. But when it comes to the machine language there is nothing called ‘Employee’. All these are business objects (domain specific objects ). Java allowed you to use those domain specific words on your program. But when it comes to the JVM level, it doesn’t understand these domain specific words.

So JVM replaces those symbolic link with a direct links before it reaches the machine level.

Let’s say you have :

Student s = new Student();

Then you call some method saying preparation student and you pass this student object there. So, In resolution part JVM replace everywhere which you use the student with the specific memory location which reserved by this newly created student object.

3. Initialization

It will assign real value.

Also, it will execute your static blocks.

In JVM it is always flexible for the implementation. They can do these phases sequentially or parallelly. But there is a limitation, JVM enforce to the implementation that initialization phase must do before each class active use.

What is the active use of the class ?

It’s considered as active use of a class if one of these happens :

  1. If you processed ‘new’ keyword

Eg : Employee e = new Employee()

2. If the class has a particular static method and you invoke it

Eg : Employee.verify()

Here you don’t create a object like in the previous one.

3. You can assign value for static field

Eg : Employee.code = “k”;

Here also you are not creating object.

If the field is final that means you can’t assign. Even you read the final variable, it won’t consider as an active use. That is why some coding standard tools enforce you to make as a final in case if you are not replacing the value. Because when you access that variable JVM is not enforcing to create new object.

4. If your class is initial class which has a main method

5. If you are using any reflection framework methods to load the class

Eg : getInstance()

6. If JVM instantiating sub class of a class

Eg : Creating object of Manager class without creating object of Employee class

Four ways of Java can initialize (create new instance) its class

  1. new keyword

Eg : Employee e = new Employee()

2. clone() method from existing class/ object

3. Using reflection API — getIntance() method

4. Java.IO.ObjectInputStream class and calling getObject() method

The initialization process it will assign initial value into the variable. But the way it’s assigned initial value to these instance variable would be different based on how these being created.

Eg : If we use clone method to create the object, then it will get value from its SOURCE object to assign the initial value.

But if we use ObjectInputStream, it will assign value from input stream for all non-transient variable.

If you use new keyword or reflection API, it will go through the initialization process and initialization process will assign initial values to the variables.

Why parent class get initialized when you initialize your child class ?

In compile time it create a reference to parent class.

2. Memory Area

Components of Memory Area

3. Execution Engine

This component executes the bytecode which is assigned to the runtime data areas through the ClassLoader.

This has three components for executing the byte code.

  1. Interpreter
  2. JIT Compiler
  3. Garbage Collector

Reference

--

--