Introduction To Java Virtual Machine (JVM)

What is JVM?

The Java Virtual Machine (JVM) is a virtual machine that provides the runtime environment to execute Java bytecode.
The JVM does not understand Java source code directly. That’s why .java files are first compiled into .class files containing bytecode, which the JVM can interpret and execute.This bytecode is platform-independent and can run on any machine with a compatible JVM.
The JVM is responsible for controlling the execution of every Java program and enables important features such as automatic memory management and security.

It also enables important features such as:

  • Automatic exception handling
  • Garbage collection for memory management
  • Platform independence (through bytecode execution)

JVM Architecture : 

The JVM architecture consists of several key components that work together to execute Java programs:

1.Class Loader

The Class Loader loads classes into memory for execution.

There are three main types of class loaders:

  1. Boot Strap ClassLoader → Loads core Java classes from rt.jar file (inside JRE/lib). It has the highest priority.
  2. Extension ClassLoader – → Loads classes from the ext directory (jre/lib/ext).
  3. Application (System) ClassLoader → Loads classes from the application’s classpath (environment variables, project paths, etc.).

3.Method Area

Stores class-level data such as class structures, metadata, method code, and static variables. There is only one Method Area per JVM instance.

3.Heap

Stores all Java objects, their instance variables, and arrays. The heap is shared among all threads, and hence the data stored here is not thread-safe by default.

4.JVM Stack

Each thread has its own JVM stack, created when the thread starts. It stores:

  • Local variables
  • Partial results
  • Method call frames (each frame holds data, local variables, and references to objects in the heap

5.Program Counter (PC) Register

Each thread has its own PC register. It holds the address of the current JVM instruction being executed.

6.Native Method Stack

Contains instructions for native methods (non-Java code, usually written in C/C++). It supports interaction between Java applications and platform-specific native libraries.

7.Execution Engine

The Execution Engine runs the bytecode. It consists of:

  • Interpreter → Executes bytecode line by line (simple but slower).
  • JIT (Just-In-Time) Compiler → Improves performance by compiling frequently used bytecode into native machine code.
  • Garbage Collector (GC) → Automatically manages memory by removing unused objects from the heap

8.Native Method Interface (JNI)

Acts as a bridge between Java code and native applications/libraries (C, C++, etc.), allowing Java to call platform-specific methods.

9.Native Method Libraries

A collection of native libraries required by the Execution Engine. These are loaded and linked using the JNI.


📝Key Features of JVM

  • Provides platform independence by executing bytecode on any system.
  • Ensures memory management with garbage collection.
  • Handles runtime exceptions automatically.
  • Supports integration with native libraries through JNI.

.

Backend developer working with Java, Spring Boot, Microservices, NoSQL, and AWS. I love sharing knowledge, practical tips, and clean code practices to help others build scalable applications.

One comment on “Introduction To Java Virtual Machine (JVM)

Leave a Reply

Your email address will not be published. Required fields are marked *