All About JAVA Virtual Machine (JVM)

dilshan ukwattage
Nerd For Tech
Published in
5 min readMay 3, 2021

Before we start to learn about JVM (Java virtual machine) we need to get to know what a virtual machine is. So virtual means not in the reality. The machine is some device to do our work. So the virtual machine is something that does not exist. Virtual Machines can be mainly categorized into two.

  1. System Based
  2. Application based/ Process based

System based VM’s may have one or more hardware and it creates multiple environments which is completely independent of each other.

Application-based VM’s don’t have any hardware involved. But may have software that helps you to create a platform to run other programs.

Since this is application based we can consider this as a program. So JVM is an application-based VM. JVM is a specification it says how this should be done. When we install Java Runtime Environment(JRE) it will deploy the particular code to create a JVM for the particular platform.

When the moment you start a java program then it will create a JVM instance on your computer. The moment that your program exit the JVM instance also destroyed. let’s say if you are executing three Java programs on your computer at the same time. So how many JVM instance are there at that time?

The answer is 3. Because there will be 3 different JVM instance on your computer at that time.

We use javac <Filename> to compile java file into class file.

To run we use java <Filename>

So the moment you give java it means you tell to your Operating System(OS) give me a JVM instance. When JVM is creating it create a Non Demon thread. In our class there should be public static void main method. So when JVM creates it takes the main method to execute.

JVM can destroyed in two ways.

  1. If all the non demon threads are destroyed.
  2. when application called System.exit()

Inside JAVA Virtual Machine

High level picture of JVM

Inside the JVM we have mainly 3 parts.

  1. Class loader
  2. Memory area
  3. Execution engine
Inside JVM

Let’s talk about the class loader first.

1.Class Loader

Class loader main responsibility is loading the class to the memory. Other than that there are some other responsibilities. Those can be categorize in to 3 parts.

  1. Loading
  2. Linking
  3. Initialization

Loading

When JVM read the file to load it will,

  1. Read fully qualified class name

2. Read the Variable information

3. Read the immediate parent information

4. Read whether this is a class or interface

So read those information and load into main memory area. Each and every class when it’s loaded JVM create object from class type.(Only one object per class is creating)

Linking

Linking mainly divided into three parts.

  1. Verification
  2. Preparation
  3. Resolution

Verification

In verification it has a sub program called byte code verifier and it will check,

Whether this class come form a valid compiler?

Whether this class has a correct structure?

Whether this call file has correct formatting?

If above are not satisfied JVM will throw an exception called Verifier Exception. If everything fine it will go to preparation.

Preparation

Preparation means if you use any instance level variables or static variables in your class this preparation part assign a default value to that.

eg:

object -> null , int -> zero, boolean -> false

Resolution

In java we can create a class any we can put a name employee, student etc. But when it’s come to the machine code there is nothing called employee or student because everything is domain specific object. JVM also don’t understand student or employee. So before a class convert to machine level JVM replace those symbolic link with direct link.

Initialization

In the initialization part it will assign the real value and execute the static block. There is a rule every class must initialize before it do any active use. So what is a active use. Active use mean it can be,

  1. Using new keyword.
  2. Invoke static methods.
  3. Assign values for static fields
  4. Initialize class
  5. get instance
  6. Instantiate sub classes

Before any of these active use it must go through initialization phase.

There are some other things also happen in the initialization part. When the compiler compiles a class it will create a anonymous method and store the instance level variables and static variables in that method. So if we don’t have any kind of static variables or instance variables this anonymous method will not be created.

Also JVM will create a method called init for each of your constructor. in the init method(),

  1. there are can be a code to invoke some other constructors init method().
  2. there are can be byte code for the particular implementation.
  3. there are can be code to initialize instance variables .

Both of these methods can’t access by the developers.

2. Memory area

JVM memory area mainly divided into 5 sub parts.

  1. Method area — keep class information(constant pool,method data,method code,field data)
  2. Heap area — Hold all objects information
  3. Stack — Keep method and variable information
  4. PC registers — Hold the information about the next execution only if executing method is not a native method.
  5. Native method area — store the details about native information.

3. Execution engine

Execution Engine execute the byte code which is assigned to the runtime data area. Execution engine contains mainly three parts.

  1. Interpreter
  2. JIT Compiler(Just In Time Compiler)
  3. Garbage Collector

Interpreter

Interpreter interprets the byte code and execute the instruction line by line. The problem with the interpreter is that it interprets every time, even the same method multiple times, Which reduce the performance of the system.

JIT Compiler

JIT Compiler counterbalances the interpreter’s disadvantage of slow execution and improves the performance. JIT compiler is able to perform certain simple optimization while converting byte code into machine code at run time.

Garbage Collector

Garbage collector check for unused objects in the heap and removes and reclaims the memory. Garbage collector is a program in java that manages the memory automatically. It’s a daemon thread which always run in the background.

References

Execution Engine(https://www.geeksforgeeks.org/execution-engine-in-java/)

Just In Time Compiler(https://www.geeksforgeeks.org/just-in-time-compiler/)

CodeLabs(https://www.youtube.com/watch?v=bUtIIWbaFKc&list=PLD-mYtebG3X-rF1hU16AC3Rf9E-mAAkXJ)

--

--

dilshan ukwattage
Nerd For Tech

Software Engineer at IFS R&D International (Pvt) Ltd