JVM Architecture
JVM Architecture
The JVM (Java Virtual Machine) architecture is a crucial component of the Java Runtime Environment (JRE). It is responsible for executing Java bytecode and ensuring that Java programs can run on any platform without modification. JVM provides a layer of abstraction between the compiled Java code (bytecode) and the underlying hardware and operating system.
Overview of the JVM architecture
Class Loader Subsystem
The Class Loader is responsible for loading class files into the JVM from various sources (such as file systems or networks). It also verifies, loads, and links classes.
Loading: The class loader loads the bytecode from the `.class` files into memory.
Linking: After loading, the class is verified and the constant pool is created.
Initialization: Initializes static variables and executes static blocks.
Types of class loaders
Bootstrap Class Loader: Loads core Java libraries located in the `<JRE_HOME>/lib` directory.
Extension Class Loader: Loads classes from the extension directories (`<JRE_HOME>/lib/ext`).
System Class Loader: Loads classes from the classpath (i.e., user-defined classes).
Runtime Data Areas
The JVM uses several memory areas to execute Java programs. These areas store information during program execution and include the following:
Method Area: Stores class-level information such as metadata, method data, and the constant pool (which holds all constants used by the class, like strings, numbers, and references to other classes).
Heap: Stores all the objects created during program execution. It is the runtime data area from which memory is allocated for objects. Garbage collection happens here to reclaim memory.
Stack: Each thread has its own stack, which stores local variables, method calls, and partial results. The stack is created when a thread is created and is used to store method execution details.
PC Register: Each thread has its own Program Counter (PC) register, which indicates the current instruction being executed.
Native Method Stack: This area is used for executing native (non-Java) methods, often written in languages like C or C++.
Execution Engine
The Execution Engine is responsible for executing Java bytecode. It takes care of running the code and is responsible for the overall execution of the program.
Interpreter: The interpreter reads and executes bytecode instructions one at a time. It directly interprets the bytecode and executes it.
Just-In-Time (JIT) Compiler: The JIT compiler improves performance by compiling bytecode into native machine code at runtime. Once a piece of code is used frequently, the JIT compiler compiles it into native code to speed up execution.
Garbage Collector: The garbage collector automatically manages memory by identifying and clearing objects that are no longer in use, thus freeing up space in the heap for new objects.
Java Native Interface (JNI)
JNI is a framework that allows Java code running in the JVM to call and be called by native applications or libraries written in other languages like C, C++, and Python. It allows the JVM to interface with native code when necessary, such as for performance optimizations or for platform-specific functionality.
Java Native Method Interface (JVM Interface)
This interface allows Java programs to interact with native methods. It serves as a bridge for executing code outside of the Java environment.
Garbage Collection
The JVM has an automatic garbage collection process to reclaim memory by removing objects that are no longer reachable from any live thread. This helps manage memory dynamically, and several garbage collection algorithms (e.g., Serial, Parallel, CMS, G1) exist for different performance needs.
—
Java Tutorials
Java Tutorial on this website:
https://www.testingdocs.com/java-tutorial/