0% found this document useful (0 votes)
42 views7 pages

What Is JVM

Uploaded by

vibhor.gupta
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
42 views7 pages

What Is JVM

Uploaded by

vibhor.gupta
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 7

What is JVM?

JVM (Java Virtual Machine) is an abstract machine that enables your


computer to run a Java program.

When you run the Java program, Java compiler first compiles your Java
code to bytecode. Then, the JVM translates bytecode into native machine
code (set of instructions that a computer's CPU executes directly).

Java is a platform-independent language. It's because when you write Java


code, it's ultimately written for JVM but not your physical machine
(computer). Since JVM executes the Java bytecode which is platform-
independent, Java is platform-independent.

Working of Java Program


If you are interested in learning about JVM Architecture, visit The JVM
Architecture Explained.

What is JRE?
JRE (Java Runtime Environment) is a software package that provides Java
class libraries, Java Virtual Machine (JVM), and other components that are
required to run Java applications.

JRE is the superset of JVM.


Java Runtime Environment
If you need to run Java programs, but not develop them, JRE is what you
need. You can download JRE from Java SE Runtime Environment 8
Downloads page.

What is JDK?
JDK (Java Development Kit) is a software development kit required to
develop applications in Java. When you download JDK, JRE is also
downloaded with it.

In addition to JRE, JDK also contains a number of development tools


(compilers, JavaDoc, Java Debugger, etc).

Java Development Kit


If you want to develop Java applications, download JDK.

Relationship between JVM, JRE, and JDK.


What Is the JVM?
A Virtual Machine is a software implementation of a physical machine.
Java was developed with the concept of WORA (Write Once Run
Anywhere), which runs on a VM. The compiler compiles the Java file
into a Java .class file, then that .class file is input into the JVM, which
loads and executes the class file. Below is a diagram of the Architecture
of the JVM.
JVM Architecture Diagram

How Does the JVM Work?


As shown in the above architecture diagram, the JVM is divided into
three main subsystems:

1. ClassLoader Subsystem
2. Runtime Data Area
3. Execution Engine

1. ClassLoader Subsystem
Java's dynamic class loading functionality is handled by the ClassLoader
subsystem. It loads, links. and initializes the class file when it refers to a
class for the first time at runtime, not compile time.

1.1 Loading
Classes will be loaded by this component. BootStrap ClassLoader,
Extension ClassLoader, and Application ClassLoader are the three
ClassLoaders that will help in achieving it.

1. BootStrap ClassLoader – Responsible for loading classes from the


bootstrap classpath, nothing but rt.jar. Highest priority will be
given to this loader.
2. Extension ClassLoader – Responsible for loading classes which
are inside the ext folder (jre\lib).
3. Application ClassLoader –Responsible for loading Application
Level Classpath, path mentioned Environment Variable, etc.

The above ClassLoaders will follow Delegation Hierarchy Algorithm


while loading the class files.

1.2 Linking

1. Verify – Bytecode verifier will verify whether the generated


bytecode is proper or not if verification fails we will get the
verification error.
2. Prepare – For all static variables memory will be allocated and
assigned with default values.
3. Resolve – All symbolic memory references are replaced with the
original references from Method Area.

1.3 Initialization
This is the final phase of ClassLoading; here, all static variables will be
assigned with the original values, and the static block will be executed.
2. Runtime Data Area
The Runtime Data Area is divided into five major components:

1. Method Area – All the class-level data will be stored here,


including static variables. There is only one method area per JVM,
and it is a shared resource.
2. Heap Area – All the Objects and their corresponding instance
variables and arrays will be stored here. There is also one Heap
Area per JVM. Since the Method and Heap areas share memory for
multiple threads, the data stored is not thread-safe.
3. Stack Area – For every thread, a separate runtime stack will be
created. For every method call, one entry will be made in the stack
memory which is called Stack Frame. All local variables will be
created in the stack memory. The stack area is thread-safe since it
is not a shared resource. The Stack Frame is divided into three
subentities:
1. Local Variable Array – Related to the method how many
local variables are involved and the corresponding values will
be stored here.
2. Operand stack – If any intermediate operation is required to
perform, operand stack acts as runtime workspace to
perform the operation.
3. Frame data – All symbols corresponding to the method is
stored here. In the case of any exception, the catch block
information will be maintained in the frame data.
4. PC Registers – Each thread will have separate PC Registers, to hold
the address of current executing instruction once the instruction is
executed the PC register will be updated with the next instruction.
5. Native Method stacks – Native Method Stack holds native method
information. For every thread, a separate native method stack will
be created.

3. Execution Engine
The bytecode, which is assigned to the Runtime Data Area, will be
executed by the Execution Engine. The Execution Engine reads the
bytecode and executes it piece by piece.

1. Interpreter – The interpreter interprets the bytecode faster but


executes slowly. The disadvantage of the interpreter is that when
one method is called multiple times, every time a new
interpretation is required.
2. JIT Compiler – The JIT Compiler neutralizes the disadvantage of
the interpreter. The Execution Engine will be using the help of the
interpreter in converting byte code, but when it finds repeated
code it uses the JIT compiler, which compiles the entire bytecode
and changes it to native code. This native code will be used directly
for repeated method calls, which improve the performance of the
system.
1. Intermediate Code Generator – Produces intermediate
code
2. Code Optimizer – Responsible for optimizing the
intermediate code generated above
3. Target Code Generator – Responsible for Generating
Machine Code or Native Code
4. Profiler – A special component, responsible for finding
hotspots, i.e. whether the method is called multiple times or
not.
3. Garbage Collector: Collects and removes unreferenced objects.
Garbage Collection can be triggered by calling System.gc(), but
the execution is not guaranteed. Garbage collection of the JVM
collects the objects that are created.

Java Native Interface (JNI): JNI will be interacting with the Native
Method Libraries and provides the Native Libraries required for the
Execution Engine.
Native Method Libraries: This is a collection of the Native Libraries,
which is required for the Execution Engine.

You might also like