2. Virtual Machine
Virtual Machine : A simple software simulation of physical machine that is act like
physical machine but it doesn't have any physical existence.
Like : Calculator Software
Type of Virtual Machine :
1. Hardware based virtual Machine
2. Application Based or process processed virtual machine
3. Hardware Based Virtual Machine
When we create several logical system on the same computer with strong
isolation from each other so that is Hardware Based virtual Machine.
The main advantage of it is to utilization of our hardware .
Examples :
Cloud Computing
VMWare
KVM (Kernel Based Virtual machine )
4. Application Based Virtual Machine
These virtual machines acts as runtime engine to run a programming language
application.
Example
JVM : It is a runtime engine to run java application .
5. Java Virtual Machine
JVM is runtime engine to run java based application , it's part of JRE . JVM is
responsible for two activity to load and run java application .
JVM is divided into 3 components
1. Class Loader subsystem (To load .class file)
2. Memory area
3. Execution engine
7. Class Loader Subsystem
Class Loader is responsible for three activity .
1. Loading
2. Linking
3. Initializing
1. Loading :- Loading is the process of reading .class file data and store
corresponding binary data in method area.
8. Cont..
For each class file JVM will store
1. Fully qualified name of the class.
2. Fully qualified name of immediate parent.
3. Weather .class file represents class/interface/enum.
4. Methods / Constructor / Variable information.
5. Modifier information.
6. Constant information
11. Linking/Verification
● Its is process of ensuring that binary representation of class is structurally
correct or not .
○ i.e weather .class file properly formatted or not
○ Weather .class file generated by valid compiler.
● ByteCode verifier is responsible for these kind of activity .which i part of class
loader subsystem.
● If verification failed program will get RuntimeException : java.lang.VerifyError
12. Linking/Preparation
In this phase JVM will allocate memory for class level static variables and
assigned with default value.
E.g
1. For int → 0
2. For double → 0.0
3. For boolean → false
Note:Here just default values will be assigned and original value will be assigned
in initialization phase.
13. Linking/Resolution
When we compile our program we get separate file of .class file for each class ,
but they have some symbolic connection between each other ,And when we run
our program JVM loads the program classes and hooks them together in process
of dynamic linking .
14. Initialization
In this phase for static variable original value will be initialized and static block
execution will be performed from top to bottom.
Note : while Loading , Linking , Initializing if any problem occur so JVM will give
Runtimeexception re: java.lang.LinkageError
15. Types of Class Loader
Every class loader subsystem contain three type of class loader.
1. BootStrap class loader
2. Extension class loader
3. Application class loader
16.
17.
18. Class Loader Working
1. Class loader follow delegation hierarchy principal .
2. Whenever JVM come across a Particular class , First it check weather the
corresponding class already loaded or not .
3. If already loader in method area then JVM will use that loaded class.
4. Other wise subsystem class loader will load the class file.
5. Application class loader delegates request extension class loader and
extension class loader delegates to bootstrap class loader.
6. Bootstrap class loader searches in bootstrap class path (JDK/JRE/LIB). If
class is available then it will be loaded otherwise bootstrap delegate it to the
Extension class loader.
19.
20. Method Area
● Java Virtual machine method area can be used to store all class code or
method code.
● All classes bytecode is loaded and stored in this runtime area , and all static
variable create in this area .
Runtime Constant Pool
● Runtime constant pool is created out of the method area and it is created by JVM when a class or interface
is created. Runtime constant pool contains the constant_pool table which is applicable for per class or
interface. It contains the literals. JVM will throw OutOfMemoryError when it cannot find memory to allocate
to runtime constant pool.
21. Heap Memory
● JVM heap area can be used to store all the objects that are created .
● It is the main memory of JVM . all object of classes
● This is finite memory area and it can be configured at the time of setting up
runtime environment using java -Xms <size> and -Xmx<size> class-name.
● This can be expandable its own , depending upon the object creation.
22. Java Stack Area
● In stack area for every thread a runtime area will be created .
● Suppose we have t1,t2….tn memory area in the stack so for every area is
called stack frame and each stack frame have three parts Local variable
array ,Operand stack , frame data.
● All local runtime variable is stored in the stack.
23. PC Registers
For every thread there will be sperapare memory area in the pc register suppose
we have pc register for t1 for t2 … fro tn
PC Register in java virtual machine will contain the instruction of next address
that has to be execute.
Native Method stack
It contain all native method used in application .
24. Execution Engine
The execution engine of JVM is responsible for execution the program and it
contain two parts.
1. Interpreter
2. JIT Compiler
Interpreter : The java compiler translate Java program into java bytecode . Java
interpreter is used to run the compiled java bytecode program.
25. JIT
JIT compile part of bytecode that have similar functionality , so in that way it
reduces the amount of time needed for compilation .
It is used to improve the performance .