What Is JVM (Java Virtual Machine) – JVM Architecture

Java Virtual Machine (JVM) is the heart of the entire execution process of the Java program. It is a software engine that provides the runtime environment necessary for Java programs to execute.

In other words, the JVM is an abstract computer machine that is responsible for executing Java bytecode (a highly optimized set of instructions) on a specific hardware and operating system platform. Because of this abstraction, Java achieves its famous feature: “Write Once, Run Anywhere” capability.

The JVM is also commonly referred to as the Java runtime system. It is a core part of the Java ecosystem that makes Java platform-independent, secure, and optimized for performance.

JVM Specification


The JVM specification is a fundamental part of the Java SE (Standard Edition) specification, which is currently maintained under the Java Community Process (JCP) by Oracle Corporation and the OpenJDK community. Different vendors provide their own JVM implementations, all of which strictly follow this Java Virtual Machine Specification.

Since JVM is platform-dependent, it is available for various hardware and operating systems. The most popular and widely used JVM implementation is HotSpot, which is part of the OpenJDK project.

HotSpot is available on many operating systems such as Windows, Linux, macOS, and Solaris. You cannot run Java program unless a compatible JVM is available for the underlying hardware and operating system.

Internal Architecture of Java Virtual Machine (JVM)


The Java Platform consists mainly of the JDK, which internally contains the JVM and required runtime libraries. The JVM is implemented as a core component of the Java Runtime Environment (JRE), which also includes essential class libraries and supporting files required to run Java programs.

Conceptually, the Java platform includes the following components:

  • JDK (Java Development Kit)
  • JRE (Java Runtime Environment)
  • JVM (Java Virtual Machine)

Major Components of Java Virtual Machine (JVM)

The Java Virtual Machine (JVM) consists of the following five major components:

  1. Class Loader Subsystem
  2. Runtime Data Areas
  3. Execution Engine
  4. Java Native Interface (JNI)
  5. Native Method Libraries

The internal architecture of JVM is shown in the figure below.

Internal Architecture of Java Virtual Machine (JVM) diagram

1. Class Loader Subsystem

The class loader subsystem of JVM is responsible for loading .class files (compiled Java bytecode) into memory. The class loading process occurs in three main stages:

  • Loading: The JVM locates and loads the binary representation of a class for a given class name. The class loader reads the .class file and creates a Class object in memory.
  • Linking: Linking consists of three sub-steps:
    • Verification – The bytecode verifier verifies whether all bytecode instructions are proper or not. If it finds any instruction suspicious or violate JVM safety rules, the further execution process is rejected immediately.
    • Preparation – During this step, JVM allocates memory for static fields in the method area and assigns them with their default values.
    • Resolution – During this step, JVM replaces symbolic references in the constant pool of a class with direct references.
  • Initialization: The JVM executes the class initialization method (<clinit>), which sets initial values to static variables and executes static initializer blocks.

2. Runtime Data Areas

JVM uses several memory areas during execution of the program that are called runtime data areas. These memory are divided into five sub-components:

  • Method area
  • Heap
  • Stack
  • PC register
  • Native method stacks

a) Method Area:

  • Method area (Class area) is a block of memory that stores class data, runtime constant pool, field data, method data, and static variables of the Java program.

b) Heap:

  • The heap is a runtime data area of the JVM where memory is dynamically allocated for all objects (instances) and arrays.
  • It is created when the JVM starts and remains available for the entire lifetime of the Java application.
  • The Heap is shared among all threads, which allows objects created by one thread to be accessed by other threads.
  • Memory management in the Heap is handled automatically by the Garbage Collector, which frees up memory occupied by objects that are no longer in use.

c) Stack:

  • The method bytecode is stored in the Method area. However, during the execution of a method, it requires some more memory to store local variables, results, and return values. This memory is allocated on Java Stacks.
  • A Stack in Java is a runtime data area where Java methods are executed. Each thread has its own private stack, which is created when the thread starts.
  • Within a Java stack, a stack frame is created for every method invocation (method call). Each stack frame contains:
    • Local variables
    • Operand stack
    • Reference to the runtime constant pool
    • Return information
  • Every time a method is called, a new stack frame is pushed (created) onto the stack. When the method execution completes, the corresponding stack frame is popped (destroyed).

d) PC Register:

  • The Program Counter (PC) register is a small memory area associated with each thread that stores the address (or index) of the JVM bytecode instructions currently being executed.

e) Native Method Stack:

  • Methods of Java program are executed on the stack memory.
  • Similarly, native methods (written in languages such as C or C++) used in the program are executed on the Native Method Stack.
  • To execute native methods, the JVM uses native method libraries.
  • These libraries are loaded and linked to the JVM through the Java Native Interface (JNI).
  • The JNI provides a standard framework for Java code to interact with native code and platform-specific features.

3. Execution Engine

  • The Execution Engine is a core component of the JVM which is responsible for executing the bytecode loaded into memory at runtime.
  • Execution engine consists of three main components: Interpreter, JIT (Just-In-Time) compiler, and Garbage Collector.
    • Interpreter – Reads and executes Java bytecode instructions one by one.
    • JIT Compiler – Converts bytecode into native code dynamically at runtime to improve speed and performance of the execution of the program. It helps to reduce the amount of time needed for the execution of the program.
    • Garbage Collector – Helps to clean up unused memory to improve the efficiency of the program.

4. Java Native Interface (JNI)

  • Java Native Interface (JNI) is a programming framework that allows Java code running inside the Java Virtual Machine to interact with native applications and libraries written in native languages such as C and C++.

5. Native Method Libraries

  • Native Method Libraries are dynamic libraries that provide the implementation code for methods declared as native within Java classes.
  • These libraries contain platform-specific code written in languages such as C or C++. They are loaded by the JVM at runtime.
  • Some common examples of native method library file formats include:
    • *.dll on Windows
    • *.so on Linux
    • *.dylib on macOS
  • These libraries are accessed through the Java Native Interface (JNI), which allows Java code to interact with native applications and libraries written in C and C++.

How JVM Works Internally?


Java Virtual Machine performs the following operations for execution of the program, as shown in the figure below:

How JVM works Internally?

Step 1: Compilation (Outside JVM)

When you write a Java program, the Java compiler (javac) converts the source code (.java file) into .class file containing platform-independent bytecode instructions. This compilation occurs outside the JVM.

Step 2: Class Loading

The generated .class file is then passed to the Class Loader Subsystem of the JVM, as shown in the below diagram. The Class Loader Subsystem in the JVM performs the following tasks:

  • Loading
  • Linking
    • Verification
    • Preparation
    • Resolution
  • Initialization

Step 3: Runtime Data Areas Creation

When the JVM starts, it creates several runtime data areas in the memory that are used during the execution of the program. These areas store various types of data, intermediate results, and execution state. The runtime data areas include:

  • Method Area (Metaspace)
  • Heap
  • Java Stack
  • PC Register
  • Native Method Stack

These areas inside the memory exist throughout the lifecycle of the JVM and are not created per class.

Step 4: Bytecode Execution

The Execution Engine executes the bytecode using:

  • Interpreter (Executes instruction by instruction)
  • JIT Compiler (Converts frequently used bytecode into native code)
  • Garbage Collector (Automatically freesup unused memory)

Step 5: Runtime Environment Support

During the execution of program, the Java Virtual Machine (JVM) provides a complete runtime environment, which includes:

  • Memory management
  • Thread management
  • Exception handling
  • Security
  • Native method support via JNI

How Interpreter and JIT Compiler Work Simultaneously in Java?


In modern JVM implementations such as HotSpot, the Interpreter and Just-In-Time (JIT) Compiler work together using adaptive optimization to balance fast application startup and high performance. Look at the steps below:

1. Initial Execution Using Interpreter

  • When a Java program starts, the JVM executes bytecode using the Interpreter.
  • The Interpreter reads and executes bytecode instruction by instruction without converting it into native machine code.
  • This is a slow process but allows the program to start quickly.

2. Monitoring (Profiling)

  • While the code is being interpreted, the JVM continuously monitors execution behavior. It tracks metrices such as:
    • How often a method is called?
    • How many times a loop is executed?

3. JIT Compilation

  • Based on this profiling data, the JVM identifies “hot spots”, which are sections of bytecode that are executed frequently.
  • These hot spots are then compiled by JIT compiler into highly optimized, native machine code.
  • The blocks of code that are executed frequently given to the JIT compiler are called hotspots in Java.
  • This native code runs much faster than interpreted bytecode, significantly improving overall application performance.
  • Thus, JIT compiler increases the speed of the execution process.

This dynamic combination of interpretation, runtime profiling, and JIT compilation is known as Adaptive Optimization. This feature in Java really improves the execution time of the program at runtime, especially when it is to be executed multiple times.


Conclusion

The Java Virtual Machine (JVM) is the core component of the Java Ecosystem that enables Java’s powerful feature “write once, run anywhere”. It acts as an abstract machine that bridges between Java programs and the underlying hardware by executing platform-independent bytecode.

With the JVM, Java applications run reliably, efficiently, and securely across different operating systems. We hope this tutorial has helped you understand the JVM architecture, including its major components such as the Class Loader Subsystem, Runtime Data Areas, Execution Engine, JNI, and Native Method Libraries. We also understood how the JVM internally works.

In the next tutorial, we will understand the Java compiler and how it works in converting Java source code into bytecode.
Thanks for reading!!!

DEEPAK GUPTA

DEEPAK GUPTA

Deepak Gupta is the Founder of Scientech Easy, a Full Stack Developer, and a passionate coding educator with 8+ years of professional experience in Java, Python, web development, and core computer science subjects. With strong expertise in full-stack development, he provides hands-on training in programming languages and in-demand technologies at the Scientech Easy Institute, Dhanbad.

He regularly publishes in-depth tutorials, practical coding examples, and high-quality learning resources for both beginners and working professionals. Every article is carefully researched, technically reviewed, and regularly updated to ensure accuracy, clarity, and real-world relevance, helping learners build job-ready skills with confidence.