JVM Architecture
The Java Virtual Machine (JVM) is a crucial component of the Java platform, responsible for executing Java bytecode. Here’s a detailed explanation of the JVM architecture:
1. Class Loader
The Classloader subsystem of the JVM is responsible for loading class files (.class) in Memory area. It consists of three main components:
- Bootstrap Class Loader : Loads the core Java libraries (rt.jar).
- Extension Class Loader : It loads classes from the “extensions” directory (usually
lib/extin the JDK or JRE). - System/Application Class Loader : it is responsible for loading classes from the application’s classpath. The Application Class Loader loads classes from locations defined by the user, such as directories, JAR files, and other resources specified in the classpath.
Linking
Verification : Ensures the correctness of the loaded class.
Preparation : Allocates memory for class variables (static fields) and initializes them to default values.
Initialization: This is the final phase of class loading, where the JVM assigns the correct initial values to the static fields of the class and executes any static initialization blocks.
2. Runtime Data Areas
- Method Area : Shared among all threads. It stores class-level data such as class structures, method data, runtime constant pool, field and method information, and static variables.
- Heap : shared by all threads, where objects and arrays are dynamically allocated at runtime. It is the largest memory area in the JVM and is used to store all instances of classes (objects) and arrays created by the program. The Heap is divided into two primary regions: the Young Generation (for short-lived objects) and the Old Generation (for long-lived objects).
- Java Stack : it created for each thread when it is launched. It stores stack frames that hold method-specific data such as local variables, method call arguments, return values, and the current execution state. Each time a method is invoked, a new stack frame is pushed onto the thread’s Java Stack, and when the method completes, the frame is popped off..
- PC Registers : Each thread has its own PC register, which holds the address of the JVM instruction currently being executed.
- Native Method Stacks : Each thread has its own native method stack, which stores native method information. Specifically designed to support native (non-Java) methods written in languages like C or C++. These stacks hold the state of native method invocations, such as local variables, intermediate computations, and other method-specific data.
3. Execution Engine
Interpreter : Executes bytecode instructions line by line.
JIT Compiler : Compiles bytecode into native machine code for faster execution. There are different types of JIT compilers:
- C1 (Client Compiler): Optimizes for fast startup time.
- C2 (Server Compiler): Optimizes for peak performance.
Garbage Collector : Manages memory by automatically reclaiming unused objects. There are different garbage collection algorithms:
- Serial Garbage Collector: Uses a single thread for garbage collection.
- Parallel Garbage Collector: Uses multiple threads for garbage collection.
- CMS (Concurrent Mark-Sweep) Garbage Collector: Minimizes pause times by doing most of the work concurrently with the application threads.
- G1 (Garbage-First) Garbage Collector: Designed for multiprocessor machines with large memories.
4. Java Native Interface (JNI)
JNI allows Java code running in the JVM to interoperate with applications and libraries written in other languages, such as C and C++.
The Java Native Interface (JNI) is a framework in Java that allows Java code to interact with native applications and libraries written in other programming languages like C, C++, or assembly. JNI acts as a bridge between the Java Virtual Machine (JVM) and native code, enabling Java programs to call functions or use features that are not available in Java or to interact with platform-specific features like hardware or operating system functionalities.
5. Native Method Libraries
These are libraries written in languages other than Java, such as C and C++. They are used to interact with the native system functionalities.
Native Method Libraries in Java refer to external libraries written in non-Java languages, such as C, C++, or assembly, which can be called from Java code via the Java Native Interface (JNI). These libraries provide platform-specific or low-level functionality that Java itself cannot directly handle, such as hardware interaction or optimizing performance-critical operations.
When using JNI, native method libraries are loaded into the JVM using methods like System.loadLibrary() or System.load(). These libraries typically contain the implementation of native methods declared in Java classes. For example, Java might declare a method as native (without a body), and its implementation would reside in a native method library.
👏 If you found my articles useful, please consider giving it claps and sharing it with your friends and colleagues.
To read other topics



