Java Virtual Machine(JVM) Architecture

Fasrin Aleem
Nerd For Tech
Published in
7 min readOct 4, 2021

Whether or not you’ve used Java to write programs, you’ve probably heard of the Java Virtual Machine (JVM) at some point. It’s important for a Java developer to understand the architecture of the Java Virtual Machine (JVM) and how Java works in order to get the most out of it in a timely manner.

In this article, we will learn more about the JVM, how it works, and the various components that it is made of.

JVM

Before the JVM, let’s understand Java.

What is Java?

Java is a powerful general-purpose programming language. It is used to develop desktop and mobile applications, big data processing, embedded systems, and so on. According to Oracle, the company that owns Java, Java runs on 3 billion devices worldwide, which makes Java one of the most popular programming languages.

Components of Java

It is necessary to understand the components of Java in order to learn about Java. These components help in the efficient development of various Java solutions. After writing the program in Java, it must be transformed to machine language so that it can be understood and executed by the computer.

Java Components

1. Java Development Kit. (JDK)
2. Java Runtime Environment. (JRE)
3. Java Virtual Machine. (JVM)

Java Runtime Environment vs. Java Development Kit

The Java Runtime Environment (JRE) and the Java Development Kit(JDK) are the two most common Java distributions. The Java Runtime Environment (JRE) is made up of the Java Virtual Machine (JVM) and the Java class libraries. These provide the capability required to run Java programs. The JDK also includes the development tools required to write Java programs. The JDK is made up of three parts: a Java compiler, a Java virtual machine, and Java class libraries.

What is Virtual Machine?

Before we jump into the JVM, let’s revisit the concept of a Virtual Machine (VM). A single physical machine can run multiple virtual machines, each with their own operating system and applications. These virtual machines are isolated from each other.

There are mainly two categories in VM as shown in the below diagram,

VM Types
  1. System based VM (SVM) -This creates the environment for you to work for multiple users or multiple instances over that. These environments are completely independent.
  2. Application based VM (AVM) or Process based VM -In here It will allow you to run a single process as an application on a host machine. You may have the software or application which helps us to run other programs.
    eg -JVM, CLR -Common Language Runtime →Helps .net to create a VM. ,
    PVM -Parrot Virtual Machine → Create a environment for dynamic language. There is no hardware but it creates a platform or environment to run some kind of a language as input and convert it to different language or different output which someone else can understand.

What is the Java Virtual Machine?

The Java Virtual Machine (JVM) is a engine that offers a runtime environment for Java programs. It converts Java bytecode to machine code. The Java Virtual Machine (JVM) is a component of the Java Run Environment (JRE). JVM is completely a specification.

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

A Java Virtual Machine (JVM) instance will be built for each program. As a result, after the program is finished, the JVM instance is destroyed. JVM will also create a non-daemon (user threads) thread to execute the Java program.

Lifetime of a Java Virtual Machine

  • When a java application starts, a runtime instance of JVM is created. When the application completes, the instance terminates.
  • If you start three java applications at the same time, on the same computer, you will get three java virtual machine instances running
  • Each java application runs inside its own java virtual machine.
JVM per application

JVM will be destroyed under 2 circumstances such as,

  1. If there are no non-daemon threads running. At that moment, the JVM will forcefully terminate all the active daemon threads.
  2. If the Java app kills itself (by calling System.exit() method).

and obviously, JVM will be destroyed if it crashes.

Components of JVM

JVM is made up of three components, They are

  1. Class Loader -to load the class files
  2. Memory Area -to store the classes
  3. Execution Engine -to execute/provide run time environment
JVM Components

Let’s take a deeper look at each component.

1. Class Loader

It is responsible for the following three activities

  1. Loading
  2. Linking
  3. Initialization

Loading

This component will load classes for you. BootStrap ClassLoader, Extension ClassLoader, Application ClassLoader are the three class loader which will help in achieving it.

The JVM’s Classloader subsystem is in responsible of loading class files (.class) into memory. When we run a Java program, the class loader loads it first. When JVM loads a class, it stores the following information in the method area.

  • The fully qualified name of the loaded class.
  • The immediate parent class of the loaded class. (If there are no parent class JVM decides loaded class as a parent class)
  • Whether the “.class” file is a Class or Interface or Enum.
  • Details on the Modifier, Variables, and Method information, and so on.

After loading dot class file immediately JVM create an object for the loaded class on the heap memory of type java.lang.Class. This class type object is a predefined Class object in the java.lang package. A programmer can use the Class class object to get information about a class’s methods, variables, and constructors, among other things.

Linking

  1. Verify -Bytecode verifier will verify whether the generated bytecode is proper or not if verification fails we will get verification error.
  2. Prepare -For all static variables memory will be allocated and assigned with default values.
  3. Resolve -All symbolic memory references are replaced with the original references from Method Area.

Initialization

This is the final step in the Class Loading process, where all static variables are assigned their original values and static blocks are executed.

2. Memory Area

The memory area of the JVM is where data is stored. Method Area, Heap Area, Stack, Program Counter(PC) Registers, and Native Method Area are the five sub-areas of the JVM memory area.

Memory Area of JVM

Class Area -Every class’s class level data is stored here, such as the runtime constant pool, field and method data, and method code.

Heap -The heap area, which is created during virtual machine startup and from which memory is allocated for all class instances and arrays, represents the runtime data area.

Stack -In Java, stack memory is used for static memory allocation and thread execution. It’s made for each thread. When threads start, the JVM creates a separate runtime stack in which method calls are stored. For each method call, a frame will be produced and pushed into the runtime stack’s storage. When a frame’s method invocation is finished, it is destroyed.

Program Counter Register -Each thread will have its own PC Register, which will store the address of the currently executing instruction. Once the instruction is completed, the PC register will be updated with the next instruction.

Native Method Area -This will store information about native methods, which are written in a language other than Java, such as C or C++. Every new thread will have its own native method stack, same like stack and PC register.

3. Execution Engine

The Execution Engine will run the byte code that has been assigned to a memory area. The Execution Engine reads the bytecode one by one and executes it. It will contain three components.

Interpreter -Reads the bytes, interprets them, and then executes them one by one. The fundamental downside of Interpreter is that when the same method is called multiple times, a new interpretation is required each time, so it will lowering the system’s performance. JIT compiler comes as a solution for it.

JIT Compiler (Just In Time Compiler) -The runtime environment includes the Just-In-Time (JIT) compiler. By compiling bytecodes to machine code at run time, it helps in the performance of Java applications.

Garbage Collector -as the name suggests, is a tool for collecting unused materials. Garbage collection takes care of this on the JVM. It keeps track of every item in the JVM heap area and removes unwanted ones.
Mark and Sweep are two simple steps used by garbage collectors:

Mark -it is where the garbage collector identifies which piece of memory is in use and which are not
Sweep -it removes objects identified during the “mark” phase

References

  1. 2021, Java Virtual Machine Architecture Explained, [Video] -https://www.youtube.com/watch?v=jnpuRvRdTgI
  2. 2020, Java Architecture and Components, [Document] -https://www.lsraheja.org/wp-content/uploads/2020/04/SYBSCIT-SEM-IV-Core-Java-Unit-I-II.pdf
  3. 2016, What is JVM, [Video] -https://www.youtube.com/watch?v=bUtIIWbaFKc

--

--