JVM JRE JDK Program Exe Flow

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

JVM, JRE & JDK

Fig. JDK, JRE & JDK

JVM (Java Virtual Machine)


JVM is an abstract machine. It provides runtime environment to execute
bytecode (Purpose of JVM).

JVM contains compiler which converts the source code to bytecode.

It also contains interpreter which converts the bytecode to native code.

JVM Internal Architecture/Internal Working of JVM


The compiler will compile .java file into a .class file.

The .class file is input to the JVM which loads & execute the .class file.

The JVM architecture is divided into 3 main sub sytems

1. Class loader subsystem


2. Run-time Data area
3. Execution engine
Class Loader System:

Class Loader System loads, links & initializes the class at the runtime, not at
compile time.

There are 3 types of class loader

1. Bootstrap CL : Responsible for loading the classes from the bootstrap


classpath, nothing but rt.jar. Highest priority will be given to this
class.
2. Extension CL : Responsible for loading the classes which are inside ext
folder(jre/lib).
3. Application CL : Responsible for loading application level CL.

The output from the class loader will be delegated to the Linking :

a) Verify : Bytecode verifier will verify whether the generated bytecode is


proper or not, if verification fails we will get verification error.
b) Prepare : For all static variables memory will be allocated & assigned
with default values.
c) Resolve : All symbolic memory-references are replaced with the
original references from method area.

The output of the loading block will be delegated to the Initialization block.

Initialization: This is the final phase of class loading, here all static
variables will be assigned with the original values & static block will be
executed.

Run-time Data Area:

There are major 5 components in Run-time Data Area:

1. Method Area : All the class level data will be stored here including
static variables. Method area is one per JVM & it is a shared resource.
2. Heap Area : All the objects & it’s corresponding instance variable &
arrays will be stored here. Heap area is also one per JVM since
Methods Area & Heap Area shares memory for multiple threads, the
data stored is not thread safe.
3. Stack Area : For every thread, a separate run-time stack will be
created. For every method call, one entry will be made in stack
memory which is called as stack frame.
All local variables will be created in stack memory. Stack area is
thread-safe since it is not shared resource.
Stack frame is divided into 3 sub entities as:
a) Local variable Array : Related to method, how many local
variables are involved & the corresponding values will be stored
here.
b) Operand Stack : If any intermediate operation is required to
perform, operand stack acts as a runtime workspace to perform
the operation.
c) Frame data : All symbols corresponding to the method will be
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 a separate register, to hold the
address of current executing instruction. Once the execution is
executed the PC register will be updated with the next instruction.
5. Native Method stack : It holds native method information. For every
thread, a separate native method stack will be created.
Execution Engine

Execution engine executes bytecode which is assigned to the Run-time


data area. The Execution engine reads the ytecode & executes one by one.

a) Interpreter : Reads the bytecode, interprets it & executes it one by


one. Interpreter interprets the bytecode faster but executes slowly.
The disadvantage if that, when one method called multiple times,
every time interpretation is required.
b) JIT compiler: For repeated code execution engine will be using JIT
compiler, which compiles the entire bytecode & changes it to
native code. This native code will be used directly for repeated
method calls, which improve the performance of the system
c) Garbage collector : It collects all unreferenced objects. It can be
called or triggered by calling System.gc(), but the execution is not
guaranteed. GC collects only those objects which are created by
using new keyword. So when we created any object without new
keyword, we can use finalize method to perform cleanup.

Java Native Interface:

It will be interacting with the native method libraries & provides the Native
libraries required for the execution engine.

Native Method Libraries :

It is the collection of the Native libraries which is required for execution


engine.

JRE (Java Run-time Environment)


JRE refers to a runtime environment in which bytecode can be executed. It
implements the JVM and provides all the class libraries and other support
files that JVM uses at runtime. So JRE is a software package that contains
what is required to run a program. Basically, it’s an implementation of the
JVM which physically exists.

JRE = JVM + Java Libraries


JDK (Java Development Kit)
JDK contains JRE which includes tools for developers. Along with JRE it
contains other development tools like a compiler (javac), a document
generator (javadoc), an interpreter/loader and other required tools for
development.

It is open source.

JDK = JRE + Development Tools

Note : JDK is platform dependent as there are separate installers for


Windows, Mac and Unix systems.

Interview Questions :
1. What is difference between JVM, JRE & JDK?
2. What is the purpose of JVM?
3. Is JDK platform independent or dependent?
4. How java ensures high performance?
5. What is JIT compiler?
Program execution flow in Java
Program execution happen in following 4 steps:

▪ Compile : In this step a compiler (javac) compiles the source code


and converts it into bytecode (converts into .class file) which is a
language understood by JVM. If there are any compile time error in
our code then these are raised at this step.

▪ Load : In this step class loader loads the .class file which contains
bytecode and stores it in memory.
▪ Verify : In this step the bytecode is checked by bytecode verifier to
validate whether the bytecode is valid or not.
▪ Execute : In this step java interpreter interprets the bytecode one at a
time and converts the bytecode to native code which is more likely to
understand by machine. Finally, it runs the program.

You might also like