avatarVinotech

Summary

The JVM architecture is a critical framework that includes class loaders, runtime data areas, an execution engine, and the Java Native Interface, all of which are essential for executing Java bytecode efficiently and securely.

Abstract

The Java Virtual Machine (JVM) is an integral part of the Java platform, designed to execute Java bytecode. It features a sophisticated architecture that begins with class loaders responsible for loading class files into memory. The JVM ensures the integrity of loaded classes through a linking process that includes verification, preparation, and initialization. Runtime data areas such as the method area, heap, Java stack, PC registers, and native method stacks are allocated to manage class-level data, object instances, thread-specific data, and native method information. The execution engine within the JVM interprets bytecode and employs JIT compilers like C1 and C2 for optimized performance. Memory management is handled by various garbage collectors, including the Serial, Parallel, CMS, and G1 garbage collectors. The JVM also incorporates the Java Native Interface (JNI) to facilitate interaction with native applications and libraries, and it supports native method libraries for accessing system-specific functionalities.

Opinions

  • The author suggests that the JVM's class loader subsystem, with its hierarchical structure of Bootstrap, Extension, and System/Application class loaders, is critical for managing class files and ensuring that the correct classes are loaded.
  • The linking process, particularly the verification step, is emphasized as a crucial security feature that ensures the correctness of the loaded class.
  • The runtime data areas are presented as meticulously designed spaces that optimize the JVM's performance and thread management, with the heap being a focal point due to its role in dynamic memory allocation for objects and arrays.
  • The execution engine's interpreter and JIT compilers are highlighted for their roles in executing bytecode and enhancing Java performance through native machine code compilation.
  • Garbage collection is portrayed as a key feature of the JVM, with different algorithms suited for various use cases, aiming to minimize pause times and efficiently manage memory, especially in multiprocessor environments.
  • The Java Native Interface (JNI) is acknowledged as an essential framework for extending Java's capabilities beyond its sandbox, allowing integration with applications and libraries written in other languages.
  • The use of native method libraries is seen as a practical solution for Java to leverage platform-specific features and functionalities that are not natively supported by the language.
  • The author encourages reader engagement by inviting them to clap and share the articles if found useful, indicating a desire for the content to reach a wider audience and to receive validation for the work presented.

JVM Architecture

The Java Virtual Machine (JVM) is a crucial component of the Java platform, responsible for executing Java bytecode. Here’s a detailed explanation of the JVM architecture:

1. Class Loader

The Classloader subsystem of the JVM is responsible for loading class files (.class) in Memory area. It consists of three main components:

  • Bootstrap Class Loader : Loads the core Java libraries (rt.jar).
  • Extension Class Loader : It loads classes from the “extensions” directory (usually lib/ext in the JDK or JRE).
  • System/Application Class Loader : it is responsible for loading classes from the application’s classpath. The Application Class Loader loads classes from locations defined by the user, such as directories, JAR files, and other resources specified in the classpath.

Linking

Verification : Ensures the correctness of the loaded class.

Preparation : Allocates memory for class variables (static fields) and initializes them to default values.

Initialization: This is the final phase of class loading, where the JVM assigns the correct initial values to the static fields of the class and executes any static initialization blocks.

2. Runtime Data Areas

  • Method Area : Shared among all threads. It stores class-level data such as class structures, method data, runtime constant pool, field and method information, and static variables.
  • Heap : shared by all threads, where objects and arrays are dynamically allocated at runtime. It is the largest memory area in the JVM and is used to store all instances of classes (objects) and arrays created by the program. The Heap is divided into two primary regions: the Young Generation (for short-lived objects) and the Old Generation (for long-lived objects).
  • Java Stack : it created for each thread when it is launched. It stores stack frames that hold method-specific data such as local variables, method call arguments, return values, and the current execution state. Each time a method is invoked, a new stack frame is pushed onto the thread’s Java Stack, and when the method completes, the frame is popped off..
  • PC Registers : Each thread has its own PC register, which holds the address of the JVM instruction currently being executed.
  • Native Method Stacks : Each thread has its own native method stack, which stores native method information. Specifically designed to support native (non-Java) methods written in languages like C or C++. These stacks hold the state of native method invocations, such as local variables, intermediate computations, and other method-specific data.

3. Execution Engine

Interpreter : Executes bytecode instructions line by line.

JIT Compiler : Compiles bytecode into native machine code for faster execution. There are different types of JIT compilers:

  • C1 (Client Compiler): Optimizes for fast startup time.
  • C2 (Server Compiler): Optimizes for peak performance.

Garbage Collector : Manages memory by automatically reclaiming unused objects. There are different garbage collection algorithms:

  • Serial Garbage Collector: Uses a single thread for garbage collection.
  • Parallel Garbage Collector: Uses multiple threads for garbage collection.
  • CMS (Concurrent Mark-Sweep) Garbage Collector: Minimizes pause times by doing most of the work concurrently with the application threads.
  • G1 (Garbage-First) Garbage Collector: Designed for multiprocessor machines with large memories.

4. Java Native Interface (JNI)

JNI allows Java code running in the JVM to interoperate with applications and libraries written in other languages, such as C and C++.

The Java Native Interface (JNI) is a framework in Java that allows Java code to interact with native applications and libraries written in other programming languages like C, C++, or assembly. JNI acts as a bridge between the Java Virtual Machine (JVM) and native code, enabling Java programs to call functions or use features that are not available in Java or to interact with platform-specific features like hardware or operating system functionalities.

5. Native Method Libraries

These are libraries written in languages other than Java, such as C and C++. They are used to interact with the native system functionalities.

Native Method Libraries in Java refer to external libraries written in non-Java languages, such as C, C++, or assembly, which can be called from Java code via the Java Native Interface (JNI). These libraries provide platform-specific or low-level functionality that Java itself cannot directly handle, such as hardware interaction or optimizing performance-critical operations.

When using JNI, native method libraries are loaded into the JVM using methods like System.loadLibrary() or System.load(). These libraries typically contain the implementation of native methods declared in Java classes. For example, Java might declare a method as native (without a body), and its implementation would reside in a native method library.

👏 If you found my articles useful, please consider giving it claps and sharing it with your friends and colleagues.

To read other topics

JVM
Jvm Architecture
Memory Management
Garbage Collection
Heap Memory
Recommended from ReadMedium