The Nuts and Bolts of the Java Virtual Machine
Kumar Aditya
Full Stack Engineer (Spring Boot, Angular) | DevOps | AWS Solution Architect | Java | JavaScript | Blockchain | Idea To Market
The Java Virtual Machine (JVM) is a critical component of the Java programming language, providing a platform-independent environment that allows Java applications to run on any device or operating system that supports the JVM. This detailed article will explore the various components and working mechanisms of the JVM, emphasizing its architecture, execution process, and memory management.
Overview
The JVM enables Java’s "write once, run anywhere" capability by abstracting the underlying hardware and operating system details. It performs several tasks, including loading code, verifying code, executing code, and providing runtime environments. The main components of the JVM are:
1. Class Loader Subsystem
The Class Loader Subsystem is responsible for loading class files. It locates, loads, and links class files into the JVM memory.
Bootstrap Class Loader: The parent of all class loaders, it loads the core Java libraries from the rt.jar file.
Extension Class Loader: Loads classes from the extension directories (jre/lib/ext).
Application Class Loader: Loads classes from the application's classpath.
Verification: Ensures the correctness of the bytecode and compliance with the JVM specifications, preventing the execution of malformed or unsafe code.
Preparation: Allocates memory for class variables and sets them to default values.
Resolution: Converts symbolic references in the class files to direct references.
2. Runtime Data Areas
The JVM organizes memory into several runtime data areas that are used during the execution of Java programs.
领英推荐
3. Execution Engine
The Execution Engine is responsible for executing the bytecode of the loaded classes. It includes several components:
4. Java Native Interface (JNI)
The JNI is a framework that allows Java code running in the JVM to call and be called by native applications and libraries written in other languages such as C, C++, and assembly. This enables Java applications to interact with native system resources and libraries, providing greater flexibility and integration capabilities.
5. Java Virtual Machine Tools Interface (JVM TI)
JVM TI provides a programming interface that allows tools to inspect the state and control the execution of applications running in the JVM. It is useful for developing debugging, profiling, and monitoring tools.
JVM Workflow
The workflow of the JVM can be summarized as follows:
Detailed Example of JVM in Action
The Interpreter reads the bytecode instructions one by one and executes them.
The JIT Compiler identifies hot spots (frequently executed bytecode) and compiles them into native machine code, significantly improving execution speed.
Memory Management with Garbage Collection
Garbage collection is an automatic process in the JVM that helps in managing memory efficiently by reclaiming memory occupied by objects that are no longer in use. The garbage collector works in the following steps: