Java Academy Logo

JVM Architecture

The Java Virtual Machine (JVM) is a core component of the Java Runtime Environment (JRE) that allows Java programs to run on any platform without modification.

  • Java source (.java) → compiled by javac → bytecode (.class)
  • JVM loads the bytecode, verifies it, links it, and then executes it
  • The JVM can directly interpret bytecode or use JIT to turn frequently used code into fast native machine code.
  • Garbage collection runs in the background to reclaim memory from unused objects

Components of JVM Architecture

JVM

1. Class Loader Subsystem

It is mainly responsible for three activities.

JVM

a) Loading

Java only reads the .class file. It stores information about the class (its methods, variables, etc.). It creates a Class object that represents the class itself, not an object of that class.

👉 This Class object is not the same as creating an actual object (instance)

1. Bootstrap Class Loader

  • The parent of all class loaders.
  • Loads core Java classes like java.lang, java.util, etc.
  • Works with the files in the JDK's rt.jar or core modules.
  • Think of it as: "The class loader that loads Java's most basic and essential classes."

2. Extension (or Platform) Class Loader

  • Child of the bootstrap loader.
  • Loads classes from the extensions directory, like: JAVA_HOME/jre/lib/ext/
  • Think of it as: "The class loader that loads extra libraries added to Java."

3. Application (or System) Class Loader

  • Child of the extension loader.
  • Loads classes from the application's classpath, such as: your .class files, JARs, or bin/ folder.
  • Think of it as: "The class loader that loads your program's classes."

b) Linking

Responsible for preparing the loaded class for execution. It includes three steps:

  • Verification: Ensures the bytecode follows JVM rules and is safe to execute.
  • Preparation: Allocates memory for static variables and assigns default values.
  • Resolution: Converts symbolic references into direct references in memory.

c) Initialization

  • Assigns actual values to static variables.
  • Executes static blocks defined in the class.

2. JVM Memory Areas

Method Area:

Stores class-level information like class name, parent class, methods, variables, and static data. Shared across the JVM.

Heap Area:

Stores all objects. Shared across the JVM.

Stack Area:

Each thread has its own runtime stack; stores method calls, local variables in stack frames. Destroyed when the thread ends.

PC Register (Program Counter):

Each thread has one. It stores the address of the instruction the thread is currently running. Helps the thread know "what to run next."

Native Method Stack:

Used when Java calls non-Java (native) code, like C/C++ methods. Each thread gets its own native stack. It stores data needed to run those native methods.

3. Execution Engine

The execution engine runs the bytecode from the .class file. It reads instructions, uses data from memory, and executes them. It has three main parts:

1. Interpreter

  • Reads and executes bytecode line by line.
  • Slower if the same method runs many times because it must interpret it again each time.

2. Just-In-Time (JIT) Compiler

  • Speeds up execution. Converts frequently used bytecode into native machine code.
  • So repeated methods run faster (no need to re-interpret).

3. Garbage Collector

Removes objects from memory that are no longer used.

4. Java Native Interface (JNI)

JNI lets Java work with native code written in C or C++.

It allows:

  • Java to call C/C++ functions
  • C/C++ code to call Java methods

This is useful for tasks that need hardware-specific or faster native code.

5. Native Method Libraries

These are collections of native libraries required for executing native methods.