0% found this document useful (0 votes)
106 views

JVM Architecture: Virtual Machine

This document discusses the Java Virtual Machine (JVM) architecture. It first defines virtual machines and describes two types: hardware-based and software-based. It then explains that the JVM is a software-based virtual machine that acts as a runtime engine for Java applications. The document goes on to describe the basic JVM architecture, including components like the classloader subsystem, method area, heap, stack, and execution engine. It also discusses various JVM memory areas and how classloaders work.

Uploaded by

intjar Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
106 views

JVM Architecture: Virtual Machine

This document discusses the Java Virtual Machine (JVM) architecture. It first defines virtual machines and describes two types: hardware-based and software-based. It then explains that the JVM is a software-based virtual machine that acts as a runtime engine for Java applications. The document goes on to describe the basic JVM architecture, including components like the classloader subsystem, method area, heap, stack, and execution engine. It also discusses various JVM memory areas and how classloaders work.

Uploaded by

intjar Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

JAVA Means DURGA SIR JAVA Means DURGA SIR

JVM Architecture
1) Virtual Machine
2) Types of Virtual Machines
3) Basic JVM Architecture

4) ClassLoader Sub Systm


 Loading
 Linking
 Initialization

5) Types of ClassLoaders
 Boot Strap ClassLoader
 Extension ClassLoader
 Application ClassLoader

6) How ClassLoader Works?

7) Customized ClassLoader
 Need of Customized ClassLoader
 Pseudo Code to Define Customized
ClassLoader

8) Various Memory Areas of JVM


 Method Area
 Heap Area
 Stack Memory
 PC Registers Area
 Native Method Stacks Area

9) Importance of Runtime Class

10) Program to Display Statistics of Heap Memory


 MaxMemory
 TotalMemory
 FreeMemory

11) How to Set Maximum and Minimum Heap Size

12) Execution Engine


 Interpreter
 JIT Compiler

13) Java Native Interface (JNI)


14) Class File Structure

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

JAVA Means DURGA SIR JAVA Means DURGA SIR

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

Method Heap Stack PC Native


Area Area Area Registers Method
Stacks
Types of Virtual Machines
Various Memory
There are 2 Types of Virtual Machines Areads of JVMe Data
1) Hardware Based OR System Based Virtual Machines
2) Software Based OR Application Based OR Process Based Virtual Machines Native
Native Native
Execution
Method Method
Method
1) Hardware Based OR System Based Virtual Machines Engine
Interface Libraries
It Provides Several Logical Systems on the Same Computer with Strong Isolation from Each Libraries
Other.
Examples:
1) KVM (Kernel Based Virtual Machine) for Linux Systems
2) VMware (Virtual Machine ware) OS
3) Xen
4) Cloud Computing
The main advantage of Hard-ware based Virtual Machines is for effective utilization of hard-
ware resources.

2) Software Based OR Application Based OR Process Based Virtual Machines

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.class classClass Object for


Customer.class Customer.class
Information

Method Area Heap Area

Student s = new Student();


Class c = s.getClass();

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();

JAVA Means DURGA SIR JAVA Means DURGA SIR

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).

Note:Original Values will be assignedin Initialization Phase.


Linking
Resolution:
Verification
 It is the Process of Replaced Symbolic References used by the Loaded Type with Original
References.

 Symbolic References are Resolved into Direct References by searching through Method Loading Preparation Initialization
Area to Locate the Referenced Entity.

Resolution

Loading of a Java Class

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

Runtime Exception Saying java.lang.LinkageError. Of course VerifyError is child class of


LinkageError only. jdk
DK jre
lib
Types of ClassLoaders:
ext
Every ClassLoader Sub System contains the following 3 ClassLoaders. *.class

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

BootstrapClassLoader Application ClassLoader OR System ClassLoader:

 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

JAVA Means DURGA SIR JAVA Means DURGA SIR

 If the required .class is Available, then it will be Loaded. Otherwise BootstrapClassLoader


Delegates that Request to ExtensionClassLoader. Example:
 ExtensionClassLoader will Search in Extension Class Path (jdk/jre/lib/ext). If the required
.class File is Available then it will be Loaded, Otherwise it Delegates that class Test {
Request to ApplicationClassLoader. public static void main(String[] args) {
 ApplicationClassLoader will Search in Application Class Path (Current Working System.out.println(String.class.getClassLoader());
Directory). If the specified .class is Already Available, then it will be Loaded. System.out.println(Student.class.getClassLoader());
Otherwise we will get Runtime Exception Saying ClassNotFoundExceptionOR System.out.println(Test.class.getClassLoader());
NoClassDefFoundError. }
}

 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.

Default Class Loader


Stuedent s1 = new Student();
:
Use :
Load :
Use Stuedent s10 = new Student();
Student.class
:
Use :
:
Stuedentsn = new Student();
nd nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
162  040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
163  040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
Customized Class Loader
Load Stuedent s = new Student();
Student.class :
:
JAVA Means DURGA SIR JAVA Means DURGA SIR

 Total JVM Memory organized in the following 5 Categories:


How to Define Our Own ClassLoader? 1) Method Area
2) Heap Area OR Heap Memory
We can Define Our Own Customized ClassLoader by extending java.langClassLoader Class. 3) Java Stacks Area
4) PC Registers Area
5) Native Method Stacks Area

1) Method Area:

 Method Area will be Created at the Time of JVM Start- Up.


 It will be Shared by All Threads (Global Memory).
 This Memory Area Need Not be Continuous.
 Method area shows runtime constant pool.
 Total Class Level Binary Information including Static Variables Stored in Method
Area.

Pseudo Code to Define Customized Class Loader 1) Class Data 2) Class Data

public class CustomClassLoader extends ClassLoader{


public Class loadClass(String name) throws ClassNotFoundException{ 3) Class Data 4) Class Data
//Rad and Written Updated Class
--- 5) Class Data
---
Method Area
---
}
} 2) Heap Area :
classCustomClassLoaderTest{
public static void main(String[] args){  Programmer Point of View Heap Area is Consider as Important Memory Area.
Dog d = new Dog(); Loaded By Default  Heap Area will be Created at the Time of JVM Start- Up.
. ClassLoader  Heap Areacan be accessed by All Threads (Global OR Sharable Memory).
.  Heap Area Nee Not be Continuous.
.  All Objects and corresponding Instance Variables will be stored in the
CustomClassLoader c = new CustomClassLoader(); Heap Area.
c.loadClass("Dog");  Every Array in Java is an Object and Hence Arrays Also will be stored in Heap
. Loaded By Memory Only.
. CusomizedClassLoade
c.loadClass("Dog"); r for Updated Version
} Object Data Object Data
}

What is the Purpose of java.lang.ClassLoader Class? Object Data Object 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

Various Memory Areas of JVM:


Program to Display Heap Memory Statistics
 Whole Loading and Running a Java Program JVM required Memory to Store Several
 A Java Application can Communicate with the JVM by using Runtime Object.
Things Like Byte Code, Objects, Variables, Etc.
nd nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
164  040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
165  040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com

JAVA Means DURGA SIR JAVA Means DURGA SIR

 Runtime Class Present in java.lang Package and it is a Singleton Class.


 We can Create Runtime Object by using  -Xmx: To Set Maximum Heap Size.
Runtime r = Runtime.getRuntime(); Eg: java -Xmx128m HeapDemo
 Once we got Runtime Object we can Call the following Methods on that Object. This Command will be Set as Maximum Heap Size as 128mb.

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.

 -Xms : To Set Minimum Heap Size.


Eg:java -Xms64m HeapDemo
This Command Set Minimum Heap Size as 64 mb.

Max Memory: 232


Total Memory: 61
Free Memory: 61
Consumed Memory: 0

 java –Xmx128m –Xms64m HeapDemo

Max Memory: 123


Total Memory: 61
classHeapDemo { 1 KB = 1024 Bytes Free Memory: 61
public static void main(String[] args) { 1MB = (1024*1024) Bytes Consumed Memory: 0
longmb = 1024*1024;
Runtime r = Runtime.getRuntime();
System.out.println("Max Memory: "+r.maxMemory()/mb); 3) Stack Memory:
System.out.println("Total Memory: "+r.totalMemory()/mb);
System.out.println("Free Memory: "+r.freeMemory()/mb);  For Every Thread JVM will Create a Separate Runtime Stack.
System.out.println("Consumed memory:"+(r.totalMemory()-r.freeMemory())/mb);  Runtime Stack will be Created Automatically at the Time of Thread Creation.
}  All Method Calls and corresponding Local Variables, Intermediate Results will be
} stored in the Stack.
Output in Terms of MB’s Output in Terms of Bytes  For Every Method Call a Separate Entry will be Added to the Stack and that Entry is
Called Stack Frame OR Activation Record.
Max Memory: 247 Max Memory: 253440  After completing that Method Call the corresponding Entry from the Stack will
Total Memory: 15 Total Memory: 15872 beRemoved.
Free Memory: 15 Free Memory: 15582  After completing All Method Calls, Just Before terminating the Thread,the Runtime
Consumed memory:0 Consumed memory:289 Stack will be destroyed by the JVM.
 The Data stored in the Stack can be accessed by Only the corresponding Thread and it
How to Set Maximum and Minimum Heap Size? is Not Available to Other Threads.

 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

 JVM Uses Operand Stack as Work Space.


 Some Instructions can Push the Values to the Operand Stack and Some Instructions can
Pop the Values from Operand Stack and Perform required Operations and Store Result
Stack Frame Structure: Once Again Back to the Operand Stack.

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 Operand 100 100 180


Stack 80

 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

JAVA Means DURGA SIR JAVA Means DURGA SIR

 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:

 This is the Central Component of JVM. Profiler


 Execution Engine is Responsible to Execute Java Class Files. Code Optimizer
 Execution Engine contains 2 Components for executing Java Classes. Garbage
 Interpreter Interpreter Collector
 JIT Compiler

Interpreter: Target Code


Generator
 It is Responsible to Read Byte Code and Interpret (Convert) into Machine Code (Native
Code) and Execute that Machine Code Line by Line.
 The Problem with Interpreter is it Interpreters Every Time Even the Same Method Target Machine
Multiple Times. Which Reduces Performance of the System. Code
 To Overcome this Problem SUN People Introduced JIT Compilers in 1.1 Version.

JIT Compiler: Java Native Interface (JNI):


 The Main Purpose of JIT Compiler is to Improve Performance. JNI Acts as Bridge (Mediator) between Java Method Calls and corresponding Native
 Internally JIT Compiler Maintains a Separate Count for Every Method whenever JVM Libraries.
Come Across any Method Call. Eg:hashCode()
 First that Method will be interpreted normally by the Interpreter and JIT Compiler
Increments the corresponding Count Variable.
 This Process will be continued for Every Method.
 Once if any Method Count Reaches Threshold (The Starting Point for a New State) Value,
then JIT Compiler Identifies that Method Repeatedly used Method (HOT SPOT).
 Immediately JIT Compiler Compiles that Method and Generates the corresponding Native
Code. Next Time JVM Come Across that Method Call then JVM Directly Use Native Code
and Executes it Instead of interpreting Once Again. So that Performance of the System will
be Improved.
 The Threshold Count Value varied from JVM to JVM.
 Some Advanced JIT Compilers will Re-compile generated Native Code if Count Reaches
Threshold Value Second Time, So that More optimized Machine Code will be
generated.
 Profiler which is the Part of JIT Compiler is Responsible to IdentifY HOT SPOTS.
Note:

 JVM Interprets Total Program Line by Line at least Once.


 JIT Compilation is Applicable Only for Repeatedly invoked Methods. But Not for Every
Method.

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

 The 1st 4 Bytes of Class File is Magic Number.


 This is a Predefined Value to Identify Java Class File.
 This Value should be 0XCAFEBABE.
 JVM will Use this Magic Number to Identify whether the Class File is Valid OR Not i.e.
whether it is generated by Valid Compiler OR Not.

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.

2) Minor_Version and Major _Version this_class: Test


super_class: java.lang.Object
 Minor and Major Versions Represents Class File Version.
 JVM will Use these Versions to Identify which Version of Compiler Generates Current Test.class
.class File

M.m 1.4 V 1.5 V 1.6 V 1.7 V


8) interface_count:It Represents Number of Interfaces implemented by Current Class File.

Major Version Minor Version 48.0 49.0 50.0 51.0


9) interface[]:It Represents the Names of Interfaces which are implemented by Current Class
File.

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

JAVA Means DURGA SIR

14 )attributes_count:It Represents Number ofAttributes Present in the Current


Class File.

15 )attributes[]: It Provides Information About All Attributes Present in the Current


Class File.

Java Source File ClassLoader Sub System


(.java File) Loading Linking

Bootstrap ClassLoader Verification


Java Compiler
(javac)
Extension ClassLoader Preparation Initialization

Java Class File


Application ClassLoader Resolution
(.class) Byte Code

PC Registers Native Method


Method Area Heap Area Stack Memory Area Stacks Area
t1 t2 tn PC Register t1 t2 tn
Class Class Object Object
Data Data Data Data For t1
::::::::::::::
Class Class Object Object
PC Register
Data Data Data Data
For tn
Local Variable Array Frame Data
Operand Stack

VARIOUS MEMORY AREAS OF JVM

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

You might also like