JVM Architecture: Virtual Machine
JVM Architecture: Virtual Machine
JVM Architecture
1) Virtual Machine
2) Types of Virtual Machines
3) Basic JVM Architecture
5) Types of ClassLoaders
Boot Strap ClassLoader
Extension ClassLoader
Application ClassLoader
7) Customized ClassLoader
Need of Customized ClassLoader
Pseudo Code to Define Customized
ClassLoader
nd nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
152 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
153 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
Virtual Machine:
Basic JVM Architecture
It is a Software Simulation of a Machine which can Perform Operations Like a
Physical Machine.
ClassLoader
Class Files
Sub System
These Virtual Machines Acts as Runtime Engines to Run a Particular Programming Language
Application.
Examples:
1) JVM Acts as Runtime Engine to Run Java Applications
2) PVM (Parrot VM) Acts as Runtime Engine to Run Scripting Languages Like PERL.
3) CLR (Common Language Runtime) Acts as Runtime Engine to Run .Net Based
Applications.
JVM
JVM is the Part of JRE.
JVM is Responsible to Load and Run Java Applications.
nd nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
154 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
155 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGA SIR JAVA Means DURGA SIR
ClassLoader Sub System: Program to print methods and variables information by using Class object:
ClassLoader Sub System is Responsible for the following 3 Activities. importjava.lang.reflect.*;
1) Loading class Student {
2) Linking private String name;
Verification privateintrollNo;
Preparation
public String getName() {
Resolution
return name;
3) Initialization
}
1) Loading: public void setRollNo(introllNo) {
this.rollNo = rollNo;
} Student
Loading Means Reading Class Files and Store Corresponding Binary Data in Method Area. public void Student.setRollNo(int)
}
For Each Class File JVM will Store the following Information in Method Area. publicjava.lang.StringStudent.getName()
1) Fully Qualified Name of the Loaded Class OR Interface ORenum. class Test1 {
public static void main(String args[]) { privatejava.lang.String Student.name
2) Fully Qualified Name of its Immediate Parent Class.
Student s = new Student(); privateintStudent.rollNo
3) Whether .class File is related to Class OR Interface OR enum.
4) The Modifiers Information Class c = s.getClass();
5) Variables OR Fields Information System.out.println(c.getName());
6) Methods Information Method[] m = c.getDeclaredMethods();
7) Constant Pool Information and so on. for (int i=0; i<m.length; i++)
System.out.println(m[i]);
After loading .class File Immediately JVM will Creates an Object of the Type class Class to Field[] f = c.getDeclaredFields();
Represent Class Level Binary Information on the Heap Memory. for (int i=0; i<f.length; i++)
System.out.println(f[i]);
}
Student.class Student.class }
Information classClass Object for
Student.class
Used
By
Programmer
Customer c = new Customer(); In the Above Example by using Student class Class Object we can get its Methods and Variable
Class c1 = c.getClass(); Information.
Note: For Every loaded .class file Only One Class Object will be Created, even though we are
using Class Multiple Times in Our Application.
class Test2 {
The Class Object can be used by Programmer to get Class Level Information Like Fully public static void main(String args[]) {
Qualified Name of the Class, Parent Name, Methods and Variables Information Etc. Student s1 = new Student();
Student s2 = new Student();
nd nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
156 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
157 040 Class c1 = s1.getClass();
– 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
Class c2 = s2.getClass();
2) Linking:
class Test {
Linking Consists of 3 Activities public static void main(String[] args) { Test.class
1) Verification String s = new String("Durga"); String.class
2) Preparation Student s1 = new Student(); Student.class
3) Resolution } Object.class
}
Verification: For the Above Class, ClassLoadersub system Loads Test.class, String.class,Student.class,
It is the Process of ensuring that Binary Representation of a Class is Structurally Correct andObject.class.
OR Not. The Names of these Class Names are stored in Constant Pool of Test Class.
That is JVM will Check whether .class File generated by Valid Compiler OR Not.i.ewhether In Resolution Phase these Names are Replaced with Actual References from Method Area.
.class File is Properly Formatted OR Not.
Internally Byte Code Verifier which is Part of ClassLoader Sub System is Responsible for
this Activity.
If Verification Fails then we will get Runtime Exception Saying java.lang.VerifyError.
3) Initialization:
In this Phase All Static Variables will be assigned with Original Values and Static Blocks will
be executed from fromtop to bottom and from Parent to Child.
Preparation:
In this Phase JVM will Allocate Memory for the Class Level Static Variables and
Assign DefaultValues (But Not Original Values).
Symbolic References are Resolved into Direct References by searching through Method Loading Preparation Initialization
Area to Locate the Referenced Entity.
Resolution
Note: While Loading, Linking and Initialization if any Error Occurs then we will get
nd nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
158 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
159 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGA SIR JAVA Means DURGA SIR
1) BootstrapClassLoader OR PrimordialClassLoader
2) ExtensionClassLoader This ClassLoader is implemented in Java and the corresponding .class File Name is
3) ApplicationClassLoader OR SystemClassLoader sun.misc.Launcher$ExtClassLoader.class
This ClassLoader is Responsible to load classes from jdk\jre\lib folder. It is the Child of Extension ClassLoader.
All core java API classes present in rt.jar which is present in this location only. Hence all This ClassLoader is Responsible to Load Classes from Application Class Path (Current
API classes (like String, StringBufferetc) will be loaded by Bootstrap class Loader only. Working Directory).
It Internally Uses Environment Variable Class Path.
Application ClassLoader is implemented in Java and the corresponding .class File Name
issun.misc.Launcher$appClassLoader.class
Bootstrap ClassLoader
Extension ClassLoader
Application ClassLoader
Location:
JDK
JRE
Lib
*.jar
(rt.jar) How Java ClassLoader Works?
This Location is Called BootstrapClassPath.
That is BootstrapClassLoader is Responsible to Load Classes fromBootstrapClassPath. ClassLoader follows Delegation Hierarchy Principle.
BootstrapClassLoader is by Default Available with the JVM. Whenever JVM Come Across a Particular Class, first it will Check whether the
It is implemented in Native Languages Like C and C++. corresponding Class is Already Loaded OR Not.
If it is Already Loaded in Method Area then JVM will Use that Loaded Class.
If it is Not Already Loaded then JVM Requests ClassLoaderSub System to Load that
Particular Class.
Extension ClassLoader: Then ClassLoaderSub System Handovers the Request to ApplicationClassLoader.
ApplicationClassLoader Delegates that Request to ExtensionClassLoader and
It is the Child of Bootstrap ClassLoader. ExtenstionClassLoader in-turn Delegates that Request to BootstrapClassLoader.
ThisClassLoader is Responsible to Load Classes from Extension Class Path. BootstrapClassLoader Searches in Bootstrap Class Path for the required .class File
Location:jdk\jre\lib\ext (jdk/jre/lib)
nd nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
160 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
161 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
For String Class: From Bootstrap Class Path by Bootstrap ClassLoader Output is null
Bootstrap ClassLoader For Student Class: From Extension Class Path by Extension ClassLoader Output is
sun.misc.Launcher$extClassLoader@1234
Searches Bootstrap Class Path
Delegates In (%JAVA_HOME%\jre\lib\rt.jar) For Test Class:From Application Class Path by Application ClassLoader Output is
sun.misc.Launcher$appClassLoader@3456
Note: Assume that Student.class Present in Both Extension Class Path and Application Class
ExtensionClassLoader
Path and Test.class Present in Only in Application Class Path.
Note:
Searches Extension Class Path Bootstrap ClassLoader is Not Java Object. Hence we are getting null in the 1st Case but
Delegates In (%JAVA_HOME\jre\lib\ext) Extension ClassLoader and Application ClassLoader are Java Objects and Hence we get
Proper Output in remaining 2 Cases.
Request [email protected]_of_Hashcode
ClassLoader
Sub System
ApplicationClassLoader ClassLoader Subsystem will give Highest Priority for Bootstrap Class Path and then
Extension Class followed by Application Class Path.
Searches Application Class Path
What is the Need of Customized ClassLoader?
In (Environment Variable Class Path)
Default ClassLoader will load .class Files Only Once Eventhough we are using
Multiple Times that Class in Our Program.
After loading .class File if it is modified Outside, then Default ClassLoaderwon't Load
Updated Version of Class File on Fly (Dynamically). Because .class File already there in
Method Area.
We can Resolve this Problem by defining Our Own Customized ClassLoader.
The Main Advantage of Customized ClassLoader is we can Control Class loading
Mechanism Based on Our Requirement.
For Example we can Load Class File Separately Every Time. So that Updated Version
Available to Our Program.
1) Method Area:
Pseudo Code to Define Customized Class Loader 1) Class Data 2) Class Data
This Class Act as Base Class for designing Our Own Customized ClassLoaders. Object Data
Hence Every Customized ClassLoader Class should extendsjava.lang.ClassLoader either
Directly OR Indirectly. Heap Area
1) maxMemory(): Returns Number of Bytes of Max Memory allocated to the Heap. Max Memory: 123
Total Memory: 14
2) totalMemory(): Returns Number of Bytes of Total (Initial) Memory allocated to the Heap. Free Memory: 14
Consumed Memory: 0
3) freeMemory(): Returns Number of Bytes of Free Memory Present in Heap.
Heap Memory Size is Finite, Based on Our Requirement we can IncreaseORDecrease Heap t1 t2 tn
Size.
The Default Heap Size is 64.
We can Use the following Flags with Java Command.
nd nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
166 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
167 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
--------------------
-
Stack Frame
JAVA Means DURGA SIR JAVA Means DURGA SIR
Each Stack Frame contains 3 Parts Program Before Storing After i-load 0 After i-load 1 After i-add After i-store
i – load 0 0 100 0 100 0 100 0 100 0 100
Stack Frame
i – load 1 Local Variable
Local Variable Array 1 80 1 80 1 80 1 80 1 80
i – add Array
i - store 2 2 2 2 2 180
Operand Stack
Frame Data:
Frame Data contains All Symbolic References (Constant Pool) related to that
Method.
Local Variable Array: It also contains a Reference to Exception Table which Provides corresponding catch
Block Information in the Case of Exceptions.
It Contains All Parameters and Local Variables of the Method.
Each Slot in the Array is of 4 Bytes. 4) PC (Program Counter) Registers Area:
Values of Type int, float, and Referenced Variables Occupy One Entry in that Array.
Values of Type long and double Occupy 2 Consecutive Entries in Array. For Every Thread a Separate PC Register will be Created at the Time of Thread
byte, short and char Values will be converted in to int Type before storing and Creation.
Occupy One Slot. PC Registers contains Address of Current executing Instruction.
But the Way of storing boolean Values is varied from JVM to JVM. But Most of the Once Instruction Execution Completes Automatically PC Register will be
JVM's follow One Entry OR One Slot for boolean Values. incremented to Hold Address of Next Instruction.
Eg:public void m1(inti,long l, Object o, byte b, double d){} 5) Native Method Stacks:
long double For Every Thread JVM will Create a Separate Native Method Stack.
int Object float All Native Method Calls invoked by the Thread will be stored in the corresponding
Native Method Stack.
Note:
0 1 2 3 4 5 6
Local Variable Array Method Area, Heap Area and Stack Area are considered as Major Memory Areas with
Respect to Programmers Point of View.
Operand Stack:
nd nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
168 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
169 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
Method Area and Heap Area are for JVM. Whereas Stack Area, PC Registers Area and
Native Method Stack Area are for Thread. That is
One Separate Heap for Every JVM
One Separate Method Area for Every JVM
One Separate Stack for Every Thread
One Separate PC Register for Every Thread
One Separate Native Method Stack for Every Thread
Execution Engine
Static Variables will be stored in Method Area whereas Instance Variables will be stored in JIT Compiler
Heap Area and Local Variables will be stored in Stack Area. Intermediate Code
Generator
Execution Engine:
nd nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
170 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
171 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGA SIR JAVA Means DURGA SIR
Class File Structure 3) Constant_Pool_Count:It Represents the Number of Constants Present in Constant Table
of the Class.
class File {
Magic_Number; 4) Constant_Pool[]:It Represents Information About Constants Present in Constant Table of
Minor_Version; the Class.
Major_Version;
Constant_Pool_Cont; 5) Access_Flash:It Shows the Modifiers which are declared for the Current Class OR
Constant_Pool[]; Interface.
access_Flash;
this_class; 6) this_class:It Represents the Name of the Class OR Interface defined by Class File.
super_class;
interface_count;
interface[]; 7) super_class:It Represents the Name of the Super Class Represented by Class File.
fields_count;
fields[];
Methods_count;
methods[];
attributes_count;
attributes[];
}
1) Magic _Number
Note: Whenever we are executing a Java Class if JVM Unable to Find Valid Magic Number
then we get RuntimeException Saying ClassFormatError: incompatible magic value.
Note: 10 )fields_count:It Represents Number of Fields Present in the Current Class File.
Higher Version JVM can Always Run Lower Version Class Files But Lower Version JVM 11 )fields[]:It Provides Names of All Fields Present in the Current Class File.
can’t Run Class Files generated by Higher Version Compiler.
Whenever we are trying to Execute Higher Version Compiler generated Class File with 12 )method_count:It Represents Number of Methods Present in the Current Class
Lower Version JVM we will get RuntimeException Saying File.
java.lang.UnsupportedClassVersionError: Employee (Unsupported major.minor version
51.0) 13 )methods[]:It Returns the Name of the Method Present in the Current Class File.
nd nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
172 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
173 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
Execution Engine
I JIT Compiler
N
Intermediate Code
T Generator Java Native
E Profiler Native Method
R Interface Libraries
Code Optimizer
P Garbage
R Collector
Target Code
E Generator
T
E Target Machine
R Code
DURGASOFT,
nd
# 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
174 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com