Architecture of JVM
Last time we have seen, JDK, JRE, and JVM of Java Programming. In this blog post, we will explore the Detailed Information of Architecture of JVM, with their significance in programming development. So let's
see...
What is JVM ?
Java
Virtual Machine is the software component of JRE (Java Runtime Environment) that provides the environment for executing Java ByteCode. It is a
mandatory i.e. essential part of the Java Platform and it enables java
programs to run on a wide variety of hardware and operating systems without
requiring any modification.
The JVM is
responsible for interpreting the Java bytecode and executing it on the host
system. It provides many services to the Java application, such
as memory management, garbage collection, and exception handling. The JVM also provides a security model that allows Java applications to
run in a sandboxed environment, where they are prevented from accessing the
host system resources without permission.
Architecture of JVM
The Architecture of JVM is designed in such a way that it allows user or
developer platform-independent execution of Java Programs. The JVM
Architecture includes several components such as ClassLoader, Runtime Data
Area, Execution Engine, Native Method Interface, and Native Method Library,
etc.
Let's take a closer look at the components of the Architecture of
JVM...
A) Class Loader
The class loader is the first component of JVM architecture. It is
responsible for loading the classes written in Java Programs at
runtime. It loads classes from the file system or network and creates a
corresponding binary representation of the class in memory.
The class loader performs following three main functions :
1. Loading
Basically, the loading function is responsible for finding the binary
representation of a class file and loading it into memory so that it can
be used as a class. Class loaders search for class files within the file
system or network, and then they read the bytecode of those class files in
order to create a binary representation that corresponds to the class file
in memory.
2. Linking
There are three sub-tasks that are performed as part of the linking
function: verification, preparation, and resolution.
-
Verification : This sub-task is responsible for checking whether
the loaded bytecode is valid and complies with the specifications
provided by the JVM.
-
Preparation : A Java Virtual Machine (JVM) throws an error if the
bytecode is invalid. The preparation sub-task allocates memory for
static variables and initializes them to their default values in case
the bytecode is invalid.
-
Resolution : In the resolution subtask, symbolic references are
replaced with direct references to binary representations of the
bytecode in memory in place of symbolic references in the
bytecode.
3. Initialization
In the initialization function, the static initializer of a class is
executed, which is a block of code that executes the static initializer,
which initializes the class' static variables.
The Java Virtual Machine (JVM) uses three class loaders :
- Bootstrap Class Loader
- Extension Class Loader
- System Class Loader
In addition
to these default class loaders, Java provides a mechanism for creating
custom class loaders that can load classes from non-standard locations, such
as databases or network resources. As a result, the class loader plays a crucial role in the JVM architecture,
as it plays the role of loading and initializing classes at runtime,
allowing Java programs to load and execute classes dynamically without
having to wait for any corresponding classes to be loaded.
B) Runtime Data Area
In the JVM architecture, the Runtime Data Area is a memory area used by the
JVM to store data during program execution. The Runtime Data Area is divided
into several memory areas, including the method area, heap, stack, and
program counter.
1. Method Area
It is mainly used to store information regarding classes and methods,
including bytecode instructions and constant pool. It is shared among all threads and is created when the JVM starts up.
2. Heap Area
It is a kind of runtime data area where all the objects are stored. It can
be divided into two subparts as Young Generation and Old Generation.
3. Stack Area
It stores specific data corresponding to the methods, including local
variables and method parameters. Each thread has its own stack, which is created when the thread is
started.
4. PC Register
The PC register is specific to each thread and is updated as the thread
executes bytecode instructions. When a thread is started, the PC register is
initialized to the address of the first instruction in the thread's run()
method.
5. Native Method Stack
The Native Method Stack is a memory area in the Runtime Data Area that is
used to store data related to native method calls. Native methods are
methods that are implemented in languages other than Java, such as C or C++,
and are accessed by Java programs using the Java Native Interface
(JNI).
Overall,
the Runtime Data Area plays a critical role in the JVM architecture, as it
is responsible for managing memory allocation and freeing up memory that is
no longer needed. By using automatic memory management and garbage
collection, the JVM provides a reliable and secure runtime environment for
executing Java programs.
C) Execution Engine
In the JVM architecture, the Execution Engine is responsible for executing
bytecode instructions. Generally, the Execution Engine consists of two
components such as the Interpreter and the Just-In-Time (JIT) compiler, but
other than that it also consists of one essential component namely the
Garbage Collector. Let's learn about them...
1. Interpreter
The interpreter is used to execute bytecode instructions of a
program one by one and it also interprets them. The Interpreter reads each bytecode instruction from the bytecode array,
decodes it, and executes the corresponding operation. The Interpreter is
responsible for executing the bytecode instructions in a thread-safe and
secure manner.
2. JIT Compiler
The JIT compiler is responsible for compiling frequently executed bytecode
sequences into native machine code for improved performance. When the JIT
compiler detects a bytecode sequence that is frequently executed, it
compiles the sequence into native machine code and replaces the bytecode
sequence with the native machine code. The JIT compiler uses a number of
optimization techniques to improve the performance of the compiled
code.
3. Garbage Collector
Garbage Collector is a component of the Execution Engine that is
responsible for managing the memory used by a Java program. The Garbage
Collector automatically frees up memory that is no longer being used by the
program, ensuring that the program does not run out of memory and
crash.
Overall,
the Execution Engine plays a critical role in the JVM architecture, as it is
responsible for executing bytecode instructions efficiently and securely. By
using a combination of bytecode interpretation and just-in-time compilation,
the Execution Engine provides a flexible and high-performance runtime
environment for executing Java programs.
Native Method Interface & Library
In the JVM
architecture, the Native Method Interface (JNI) is a programming interface
that allows Java programs to access native methods and libraries written in
other programming languages such as C, C++, and assembly language. The JNI
allows Java programs to call native methods directly, and vice versa, making
it possible to integrate Java programs with other software systems and
platforms.
The Native Method Library (NML) is a
collection of pre-compiled native libraries that can be used by Java
programs. These libraries contain native methods that have been compiled for
specific hardware and operating system configurations, making it easy to
integrate Java programs with native code.
So, now it's time to end our session. We'll see you guys Next Time, stay tuned for further updates on JAVA Programming with ComputerTipsTricks.tech...!!!
Our website will definitely help you to improve your Programming Skills and Knowledge.
Thanks for Visiting...!!!
Also Visit
0 Comments
Please do not add Any Spam link in Comments !