JVM (JAVA Virtual Machine) : The JVM Performs Following Operation
JVM (JAVA Virtual Machine) : The JVM Performs Following Operation
o Loads code
o Verifies code
o Executes code
o Memory area
o Register set
o Garbage-collected heap
Native Method
Class Area Heap Stack PC Register
Stack
Native Method
Execution Engine Native Method Interface
Libraries
Components of JVM
ClassLoader
As the name suggests, it is a component responsible for Loading Class files. Loading,
Linking and initializing a class file are the major functions of the Loader. The loader
does it work in runtime.
Loading:
Basically, the loader reads the .class file and then generates the binary code and saves
it in a method area. Bootstrap Classloader, Extension Classloader, and Application
Classloader are the various ClassLoaders responsible for loading various classes.
Linking:
Three major functions like Verification, Preparation, and Resolve. It starts with .class
file verification. If verification fails, it gives a run time verification exception. Later,
Memory is allocated to the variables with default values. Then, finally, the symbolic
memory references are replaced with direct reference from the memory area.
Initializing:
This is the final part of ClassLoader. Original values are assigned to all the static
variables, followed by the execution of Static Block. This part executes from the top to
the bottom of a class.
Heap
Details of an object and instance variables are all stored here. It is a shared memory
area, which means the data stored here is not thread-safe.
One of the most relatable errors is the “OutOfMemoryError” exception, which means the
JVM cannot allocate an object in the Heap area, or memory allocation cannot be done
for the same object.
Stack
This is where a separate runtime stack is created for every new thread. Also known as a
Run-Time Stack, every time a method is called upon, all the details are stored in the
corresponding runtime stand, and after the completion of the method, these details are
removed from the stack.
PC Registers
For every single thread, a separate PC (Program Counter) register is created, which
stores the address of the current execution instruction, which, later, will be updated
with the next instruction. This memory area is quite small and is of fixed size.
The Execution Engine has three major components, which are Interpreter, JIT Compiler
and a Garbage Collector.
1. Interpreter
2. JIT Compiler
One of the most important components of the Java Runtime Environment which
enhances the Java Application performance at run time. No other component has more
impact on performance than the JIT Compiler. This is a default compiler and is activated
when any Java method is called.
3. Garbage Collector
As the name suggests, it does have something to do with garbage; Garbage Collector
simply searches for every possible object available in the JVM heap space, checks if it is
in use, and then delete the unused ones. So, it simply marks the pieces of memory
which are in use or not. Then it goes on sweeping, where it simply removes the object
marked. The best use case is that no manual memory allocation system is needed as
the Garbage Collector does the job of automatically removing unused memory space.
As this is an automatic task, no programmer has control over scheduling any time slot
for specifically cleaning tasks and requires more CPU power as it searches for object
references.