Table of contents
1.
Introduction
2.
What is JVM (Java Virtual Machine)?
3.
How Does the Java Virtual Machine(JVM) Work?
4.
JVM Architecture
5.
Components of JVM (Java Virtual Machine) Architecture
5.1.
1. ClassLoader
5.2.
2. Class (Method) Area
5.3.
3. Heap
5.4.
4. Stack
5.5.
5. Program Counter Register
5.6.
6. Native Method Stack
5.7.
7. Execution Engine
5.8.
8. Java Native Interface (JNI)
6.
JVM Memory : 
7.
Features of the JVM
8.
Frequently Asked Questions
8.1.
Why is JVM called a machine?
8.2.
What are the three components of JVM?
8.3.
How does JVM internally work?
8.4.
What does JVM do in Java?
8.5.
What is JVM vs JRE in Java?
8.6.
Is JVM present in JRE or JDK?
8.7.
What are the responsibilities of JVM?
9.
Conclusion
Last Updated: Jun 6, 2025
Easy

JVM (Java Virtual Machine) and its Architecture

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The Java Virtual Machine (JVM) is a virtual environment that allows a computer to execute Java programs and programs developed in other languages that have been compiled into Java bytecode. JVM is available for hardware and software platforms. It is the one that calls the main which is present in a java code. It is a part of JRE(Java Runtime Environment).

JVM in Java

Java applications are called WORA(Write Once Run Anywhere). It means a programmer can code in Java and execute on any other Java-enabled system without any adjustment. Java compiler produces machine code for a particular system known as Java Virtual Machine. 

What is JVM (Java Virtual Machine)?

The JVM, or Java Virtual Machine, is like the engine that allows your Java programs to run on any device. Imagine it as a translator that turns the Java code you write into a language your computer understands. This means you can write your code once and run it anywhere, whether it's on a Windows computer, a Mac, or even a smartphone. The JVM makes Java versatile and widely used for various applications.

How Does the Java Virtual Machine(JVM) Work?

First, the code is compiled and converted into bytecode. 

  • This bytecode is interpreted on different machines. Between the host system and Java source, bytecode is an intermediate language. 
  • In JVM, Java is responsible for allocating memory space. 
  • JVM performs different tasks like it loads the code, verifies it, executes the code, and provides a runtime environment. 
  • It provides definitions for the memory area, class file format, register set, garbage collector heap, fatal error reporting, etc.

JVM Architecture

The Java Virtual Machine (JVM) is a part of the Java Runtime Environment (JRE) that enables Java applications to run on any device or operating system. It provides platform independence by converting Java bytecode into machine-specific code at runtime. Here's a breakdown of its core components:

+-----------------------------+
|         Classloader         |
| +-------------------------+ |
| |   Bootstrap Classloader | |
| | (Loads rt.jar: java.lang,| |
| |  java.net, etc.)         | |
| +-------------------------+ |
|             |               |
| +-------------------------+ |
| |   Extension Classloader | |
| | (Child of Bootstrap)    | |
| +-------------------------+ |
|             |               |
| +-------------------------+ |
| | System/Application      | |
| | Classloader (Loads from | |
| | classpath)              | |
| +-------------------------+ |
+-----------------------------+
+-----------------------------+
|    Class (Method) Area      |
| (Stores per-class structures: |
| runtime constant pool, field,|
| method data, code)          |
+-----------------------------+
+-----------------------------+
|            Heap             |
| (Runtime area for object    |
| allocation)                 |
+-----------------------------+
+-----------------------------+
|            Stack            |
| (Stores frames, local       |
| variables, partial results, |
| method invocation/return)   |
+-----------------------------+
+-----------------------------+
| Program Counter Register    |
| (Holds address of current   |
| JVM instruction)            |
+-----------------------------+
+-----------------------------+
|     Native Method Stack     |
| (Holds all native methods   |
| used in the application)    |
+-----------------------------+
+-----------------------------+
|       Execution Engine      |
| +-------------------------+ |
| | Virtual Processor       | |
| +-------------------------+ |
| |       Interpreter       | |
| +-------------------------+ |
| | Just-In-Time (JIT)      | |
| | Compiler               | |
| +-------------------------+ |
+-----------------------------+
+-----------------------------+
|    Java Native Interface    |
| (Framework to interact with |
| other languages like C, C++,|
| Assembly; sends output to   |
| console, interacts with OS) |
+-----------------------------+

Components of JVM (Java Virtual Machine) Architecture

1. ClassLoader

The ClassLoader is a part of JVM that loads class files during runtime. Java has built-in classloaders:

Bootstrap ClassLoader: Loads core Java classes from rt.jar (like java.lang.String, java.util.*).

Example: When you use System.out.println(), System is loaded by the bootstrap classloader.

Extension ClassLoader: Loads classes from the ext directory (e.g., additional JARs).

System/Application ClassLoader: Loads classes from the classpath.

Example: Your custom class MyApp.java is loaded by the system classloader.

2. Class (Method) Area

This memory area stores per-class data like field info, method info, and constant pool (used during execution).

Example: When you declare a static variable, it gets stored in the method area.

3. Heap

The Heap is the memory area where all objects are created and stored.

Example: Employee emp = new Employee(); → The emp object is stored in the heap.

4. Stack

Each thread has its own stack. It stores frames, which contain method calls, local variables, and intermediate results.

Example: When a method add(int a, int b) is called, its parameters and return value are stored in the stack.

5. Program Counter Register

It holds the memory address of the current instruction being executed by the thread.

Example: If line 15 of a method is being executed, the PC register holds the memory address of that line.

6. Native Method Stack

It contains all native (non-Java) method calls used by the application.

Example: Calling a method written in C via JNI (e.g., printing to the console using native code).

7. Execution Engine

It executes the bytecode using:

Virtual Processor: Simulates a CPU for bytecode execution.

Interpreter: Executes bytecode instructions line-by-line.

Just-In-Time (JIT) Compiler: Converts bytecode to native machine code for faster execution.

Example: Frequently used code gets compiled by JIT for performance boost.

8. Java Native Interface (JNI)

A framework that allows Java code to interact with native applications written in C/C++.

Example: Java interacting with an OS-level library to access hardware or system features.

JVM Memory : 

The Java Virtual Machine (JVM) is the revolutionary tool of Java's platform-independent capabilities, that executes Java bytecode in a manner that allows the same code to run on any machine that has a compatible JVM. Memory management in JVM is crucial for optimizing application performance and avoiding common issues like memory leaks and garbage collection pauses. Let's the JVM memory in detail, like what comes under this : 

  1. Heap Memory
    The heap memory is used for allocating memory to objects and JRE classes. It is divided into two main parts: the Young Generation and the Old Generation. The Young Generation is further divided into the Eden Space, where new objects are allocated, and the Survivor Spaces (S0 and S1), which are used during minor garbage collection. The Old Generation, also known as the Tenured Space, is where long-surviving objects are stored. The garbage collector (GC) is responsible for managing the heap memory and automatically freeing up memory that is no longer being used by the application.
  2. Non-Heap Memory
    Non-heap memory is used to store per-class structures, method data, and memory required by JVM internal processing. It is divided into several areas, including the Permanent Generation (PermGen) (deprecated since Java 8), Metaspace (Java 8+), and Code Cache. PermGen was used to store class structures, method data, and interned Strings, but it has been replaced by Metaspace in Java 8 and later versions. Metaspace stores class metadata and memory required by JVM internal processing. The Code Cache is used to store compiled native code generated by the JIT (Just-In-Time) compiler. Unlike heap memory, non-heap memory is not managed by the garbage collector.
  3. Stack Memory
    Stack memory is used for storing local variables, method calls, and partial results. Each thread in the application has its own stack. Stack memory is allocated on a per-method basis and is automatically deallocated when the method returns. If the stack memory is exhausted, a `StackOverflowError` is thrown. Stack memory is generally much smaller than heap memory, and its size can be configured using the `-Xss` JVM option.
  4. Native Memory
    Native memory is the memory required by native libraries (e.g., C or C++) used in the application. This memory is managed by the operating system, not the JVM. Native memory usage can be significant in applications that heavily rely on native libraries, and it is important to monitor and manage this memory to avoid issues like memory leaks or excessive memory consumption.

Features of the JVM

The Java Virtual Machine (JVM) is a crucial component of the Java Runtime Environment (JRE) that enables Java applications to run on any device or operating system. Key features of the JVM include:

  • Platform Independence: The JVM abstracts the underlying hardware and operating system, allowing Java programs to be written once and run anywhere without modification.
  • Automatic Memory Management: The JVM handles memory allocation and garbage collection automatically, reducing the risk of memory leaks and simplifying the developer's task.
  • Security: The JVM includes a security manager that enforces access controls, protecting the system from untrusted code execution and ensuring a secure runtime environment.
  • Performance Optimization: Through Just-In-Time (JIT) compilation, the JVM converts bytecode into native machine code at runtime, enhancing execution speed and performance.
  • Multithreading: The JVM supports concurrent execution of multiple threads, enabling efficient use of CPU resources and improving application performance.
  • Exception Handling: The JVM provides a robust exception handling mechanism, allowing programs to manage errors gracefully and maintain stable operation.
  • Class Loader: The JVM dynamically loads classes at runtime, facilitating modularity and dynamic application updates.

These features collectively make the JVM a powerful and versatile platform for running Java applications.

Frequently Asked Questions

Why is JVM called a machine?

JVM is called a machine because it acts like a virtual computer that runs Java bytecode and manages system resources efficiently.

What are the three components of JVM?

The three main components of JVM are Class Loader, Runtime Data Areas (Memory), and Execution Engine.

How does JVM internally work?

JVM loads bytecode using the class loader, stores data in memory areas, and executes instructions through the execution engine.

What does JVM do in Java?

The Java Virtual Machine (JVM) executes Java bytecode, enabling platform-independent execution of Java applications. It manages memory, handles exceptions, supports multithreading, and optimizes performance through Just-In-Time (JIT) compilation, ensuring efficient and secure runtime environments for Java programs.

What is JVM vs JRE in Java?

The Java Virtual Machine (JVM) is the runtime engine that executes Java bytecode. The Java Runtime Environment (JRE) includes the JVM along with libraries and other components necessary for running Java applications, but without development tools.

Is JVM present in JRE or JDK?

The JVM is present in both JRE (Java Runtime Environment) and JDK (Java Development Kit). JRE includes only the runtime environment needed for running Java applications, while JDK includes the JRE plus development tools such as compilers and debuggers.

What are the responsibilities of JVM?

The JVM has two primary functions: to allow Java programs to run on any device or operating system (known as the "Write once, run anywhere" principle) and to manage and optimize program memory.

Conclusion

In this article, we learned about the Java Virtual Machine (JVM) and its architecture, which plays a pivotal role in Java's platform-independent capabilities. We looked into JVM memory management, which explained its structured components like Heap Memory, Stack Memory, and Code Cache. These features and their functions help us understand how the JVM executes Java bytecode efficiently, which ensures seamless application performance across various computing platforms.

 

Related article: 

Difference between JDK, JRE and JVM 

Java Ioexception

Addressing Modes of 8086

Why is Java Platform Independent

Data Warehouse Architecture

Live masterclass