Understanding JVM (Java Virtual Machine) Architecture
Dive into the architecture of the Java Virtual Machine (JVM), an abstract machine that offers a runtime environment for executing Java bytecode. JVM’s platform-independent design enables Java programs to operate seamlessly across various hardware and software platforms, making it a cornerstone of Java’s “write once, run anywhere” capability.
JVM (Java Virtual Machine) Architecture
Java Virtual Machine
JVM (Java Virtual Machine) is an abstract machine that provides a runtime environment for executing Java bytecode. It is platform-dependent and ensures that Java programs can run on different hardware and software platforms.
Internal Architecture of JVM
JVM is specified to perform:
- Loading code
- Verifying code
- Executing code
- Providing runtime environment
JVM provides definitions for:
- Memory area
- Class file format
- Register set
- Garbage-collected heap
- Fatal error reporting, etc.
JVM Architecture
The internal architecture of JVM includes:
1. Classloader
Classloader is responsible for loading class files. There are three built-in classloaders in Java:
- Bootstrap ClassLoader: Loads core Java API classes like java.lang.*
- Extension ClassLoader: Loads classes from extension directories
- System/Application ClassLoader: Loads classes from the classpath
Example
public class ClassLoaderExample {
public static void main(String[] args) {
Class<?> c = ClassLoaderExample.class;
System.out.println(c.getClassLoader());
System.out.println(String.class.getClassLoader());
}
}
Output
sun.misc.Launcher$AppClassLoader@4e0e2f2a
null
2. Class(Method) Area
Stores per-class structures such as the runtime constant pool, field and method data, and code for methods.
3. Heap
Runtime data area where objects are allocated.
4. Stack
Stores frames with local variables and partial results. It manages method invocation and return.
5. Program Counter Register
Holds the address of the currently executing JVM instruction.
6. Native Method Stack
Contains native methods used in the application.
7. Execution Engine
Includes a virtual processor, interpreter for bytecode, and Just-In-Time (JIT) compiler to enhance performance by compiling bytecode into native machine code.
8. Java Native Interface
Framework for integrating Java with other languages like C or C++, allowing interaction with native applications and OS libraries.