Main Components of JVM

Jeewaka Manohara
3 min readMar 1, 2022
JVM

JVM — Java Virtual Machine is virtual machine. Which provides a abstraction layer to run java application within its runtime environment. Though java is Platform Independent, JVM is Platform Dependent for the sole purpose of making java Platform Independent.

What is JVM?

It is the engine that converts bytecode into machine code which is written in Java applications. JVM is bundled with the JRE and what it does is, java compiler will compile the which will run on the virtual machine that java has created.

When we create a sample application in java and we execute it, this will create a JVM for this running application. Basic concept of this is that, when we run the second instance of the same application it will create a JVM for itself, it doesn't matter how many applications we run, java will create JVM per application.

When an application is executed, it will create a non-daemon thread, which is responsible for keeping the application alive. The application dies when there are no any other non-daemon threads or when we call System.exit();

How JVM Works

Above diagram shows the way that a java file is executes, now lets dive into low level architecture of this process.

JVM Architecture

JVM can be separated in 3 different components,

  1. Class Loader
  2. Memory Area
  3. Execution Engine

Now lets dive into more details,

Class Loader

This is process that will load all the class information to the memory area, there are 2 types of Class Loaders, Bootstrap Class loader and Custom Class loader.

When a class is load process it has to go through 3 stages Loading ,Linking ,Initialization.

1. Loading

In this loading process JVM needs to read several important information about that classes that are loaded, like variable names, class name and check whether is a class/interface or a abstract class. After this process is done, JVM will cerate a Object from the class type for the first time in Heap which in Memory Area.

2. Linking

In process can be divided into 3 sub processes,

2.1 Verification — In Verification process, an another sub process will run known as “bytecode verifier”, which will verify if the class file is properly written in the structure and checks for any other. If there is an error found in this process, this will throw an expectation in runtime.

2.2 Preparation — In this process, if class file has any static or instance variable it will loaded and assigned a default value.

2.3 Resolution — In this process, all the symbolic links will be replaced with direct links in the memory locations.

3. Initialization

In this process, all the static variables are assigned with original values and static blocks are executed.

Memory Area

1.Method Area — Holds the information about the class files, it contains the names of superclass, interface name and the contractors.
2.Stack — Holds the local variable information of the methods. Stack creates a new frame in when a method is invoked. And this frame is destroyed when the method is done.
3.Heap — Holds the runtime variable data of the objects
4.PC Registers — Holds the address of JVM which it is currently executing, Each thread is created with its own PC Register
5.Native Method Area — Holds all the native methods that are required for applications.

Execution Engine

The execution engine consists of three main parts. Interpreter ,JIT Compiler and the Garbage Collector. This process will start executing the codes in the classes.

Interpreter — reads the bytecode and turn it into machine code for execution.
JIT Compiler — JIT (Just In Time) Compiler , this a component of JRE which will ensure performance improves at the run time.
Garbage Collector — This helps the java application clean-up the unwanted objects in heap area.

References

JVM | Java Virtual Machine — Javatpoint

--

--