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. JVM acts as an interpreter between Java bytecode and the underlying hardware, providing Java’s famous Write Once, Run Anywhere (WORA) capability.
Java source (.java) -> compiled by javac -> bytecode (.class)
JVM loads the bytecode, verifies it, links it, and then executes it
Execution may involve interpreting bytecode or using Just-In-Time (JIT) compilation to convert “hot” code into native machine code for performance
Garbage collection runs in the background to reclaim memory from unused objects
Architecture of JVM
The image below demonstrates the architecture and key components of JVM.
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 Registers: Hold the address of the currently executing instruction for each thread.
Native Method Stacks: Each thread has a separate stack for native method execution.
3. Execution Engine
Execution engine executes the “.class” (bytecode). It reads the byte-code line by line, uses data and information present in various memory area and executes instructions. It can be classified into three parts:
Interpreter: It interprets the bytecode line by line and then executes. The disadvantage here is that when one method is called multiple times, every time interpretation is required.
Just-In-Time Compiler(JIT): It is used to increase the efficiency of an interpreter. It compiles the entire bytecode and changes it to native code so whenever the interpreter sees repeated method calls, JIT provides direct native code for that part so re-interpretation is not required, thus efficiency is improved.
Garbage Collector: It destroys un-referenced objects. For more on Garbage Collector, refer Garbage Collector.
4. Java Native Interface (JNI)
It is an interface that interacts with the Native Method Libraries and provides the native libraries(C, C++) required for the execution. It enables JVM to call C/C++ libraries and to be called by C/C++ libraries which may be specific to hardware.
5. Native Method Libraries
These are collections of native libraries required for executing native methods. They include libraries written in languages like C and C++.