JVM Architecture
JVM Architecture
The Java Virtual Machine(JVM) is the runtime engine for executing Java applications. It is responsible for invoking the main method within Java code and is a vital component of the Java Runtime Environment(JRE).
WORA
WORA (Write Once, Run Anywhere ) means that once a program is written and compiled in Java, it can be run on any platform with a Java Virtual Machine (JVM) without any modifications.
This cross-platform capability is one of Java’s key features, enabling developers to create platform-independent applications. This capability is made possible by the JVM. When a Java file is compiled, the Java compiler generates class files containing bytecode with the same class name present in the .java file. The class file can run on JVM on any machine.
Class Loader
The Class Loader subsystem is responsible for the primary activities:
- Loading
- Linking
- Initialization
Loading
Loads class files (.class) from the file system, network, or other sources. It involves reading the “.class” file, generating the corresponding binary data, and storing it in the method area. For each “.class” file, the JVM stores information such as the fully qualified name of the loaded class and its immediate parent class, whether the “.class” file is related to a class, interface, or enumeration, and information on modifiers, variables, and methods.
Linking
Combines various parts of the program. It consists of:
Verification: Ensures the loaded class file is valid and doesn’t violate Java’s security constraints.
Preparation: Once the “.class” file has been loaded, the JVM generates an object of type class to represent this file in the heap memory. Allocates memory for class variables and initializes them to default values.
Resolution: Converts symbolic references into direct references.
Initialization
Executes static initializers and initializations of static variables.
Runtime Data Areas
Method Area: Stores class structures like metadata, constant runtime pool, method data, and method code.
Heap: The runtime data area where objects are allocated.
Java Stacks: Each thread has a private JVM stack created at the same time as the thread. It stores frames, which hold local variables and partial results, and plays a part in method invocation and return.PC Registers: Each thread has its program counter (PC) register to hold the address of the current instruction being executed.
Execution Engine
Java Interpreter: Reads and executes bytecode instructions one at a time. It is simple and easy to implement but slower.
Just-In-Time (JIT) Compiler: Compiles bytecode into native machine code at runtime for better performance. The compiled code is cached and reused, which significantly improves execution speed.
Garbage Collector (GC): This device automatically manages memory by reclaiming memory used by objects that are no longer reachable.
Native Method Interface (JNI)
Provides the ability to interact with native applications and libraries written in other languages (e.g., C, C++).