0% found this document useful (0 votes)
9 views10 pages

CH-20-JVM Architecture

The document provides an overview of JVM architecture, detailing its components such as the Virtual Machine, Class Loader, and memory areas. It explains the processes involved in loading, linking, and initializing Java applications, as well as the types of Class Loaders and their hierarchy. Additionally, it covers the execution engine, including the Interpreter and JIT compiler, and introduces the Java Native Interface (JNI) for interaction with native libraries.

Uploaded by

Bl Gocher
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views10 pages

CH-20-JVM Architecture

The document provides an overview of JVM architecture, detailing its components such as the Virtual Machine, Class Loader, and memory areas. It explains the processes involved in loading, linking, and initializing Java applications, as well as the types of Class Loaders and their hierarchy. Additionally, it covers the execution engine, including the Interpreter and JIT compiler, and introduces the Java Native Interface (JNI) for interaction with native libraries.

Uploaded by

Bl Gocher
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 10

************************* JVM-ARCHITECTURE ***********************************

Virtual Machine:-its a sw simulation of a machine , which can perform operations


like a physical machine.
its not pysical peresented but can do work same as physical
machines.

Type of VM:
-----------
1. HW based / System Based VM:- used for effecting utilization of hardwares.
-------------------------------
ex: KVM(kernel based vm), VMware(virtual machine ware), Xen , Cloud computing

2. SW Based / Apps Based / Process based VM:-


----------------------------------------------
>JVM acts as Runtime Engine to run java applications.
>CLR acts as run time engine to run .Net apps.

JVM:- is part of JRE, jvm is responsible to :


----
1-Load java Applications 2- Run Java Applications

Class Loader Sub System: - is responsible to 3 activities are


------------------------
1 Loading
2 Linking
3 Initialization

EX: 2.Linking
------------------------
A. Verification
|
1. Loading -> B. Preparation -> 3. Initialization
|
C. Resoluation
------------------------

1 Loading :
-----------
> loading means "reading" .class files and
storing corresponding Binary Data in "Method Area"
>for each class file jvm will store following info in method area.
a> Full qualified name of the Loaded class
b> Full qualified name of its immediate Parent class.
c> Modifiers info...
d> Variables info..
e> Methods info..
f> Constant Pool info.

IMP_Note:
---------
> after loading .class file , immediatally JVM will Creates an Object of Class
class Type on Heap memory
to represent class level binary infomation.
> The Class Object can be used by programmer to get Class level info like
ex: Full Qualified Name of class.
Parent class name
Methods and Variables info

Ex:
first - import java.lang.reflect.*;

public static void main(String[] args)


{
Student s = new Student();

//to get class name


Class c=s.getClass();

//System.out.println("c.getName : "+s.getClass());
System.out.println("how get className : "+c.getName());

//to get availble methods in class

Method[] m=c.getDeclaredMethods();
for(int i=0; i<m.length; i++)
{
System.out.println("Method in your Class : "+m[i]);
}

// to get variable in your class

Field[] f=c.getDeclaredFields();
for(int i=0; i<f.length; i++)
{
System.out.println("Variable in your class : "+f[i]);

}
}

---------------------------

Imp-Note:
---------
> for every loaded .class file only one Class obj will be created , not more than
one.

2 Linking: it consist of 3 activities


----------
1. Verification
2. Preparation
3. Resoluation

1. Verification:-
-----------------
>internally Byte Code Verifier which is part of Class Loader is responsible for
this activity.
>processing of ensuring that Binary representation of a class is structurally
Correc or not.
or .class file is properly formatted or not.

2. Preparation: imp
---------------
> in this phase, JVM will Allocate memory for class level static var and assign
default values not original values
Note:-> original values will be assigned in Initialization phase.

3. Resoluation:
---------------
> process of replaced Symbolic references with Original References.

Ex:
class Test
{
p.s.v.m(String[] args)
String s = new String();
Student s1 = new Student();
}

Note: here ClassLoader, loads -> Test.class, String.class, Student.class,


Obeject.class
and name of these classes are stored in "Constant pool".

> in resoluation Phase these Names are replace with actual ref from Method area.

3 Initialization:
-----------------
>all Static var will be assigned with original values and
+
>static Blocks will be executed from top to bottom and from Parent to child.

ex:
2.Linking
------------------------
A. Verification
|
1. Loading -> B. Preparation -> 3. Initialization
|
C. Resoluation
------------------------

-----------------------------------------------------

*Type of ClassLoader: 3 types are


---------------------
1. BootStrapClassLoader
|
2. ExtensionClassLoader
|
3. ApplicationClassLoader / SystemClassLoader

1. BootStrapClassLoader: imp
------------------------
>responsible to load classes from -> jdk\jre\lib .*jar folder

>Notoe:-all core java api classes present in "rt.jar" , which is present in this
location,
so all api classes like String, StringBuffer will be loaded by BootStrap
class loader only.

>this location also called -BootStrapClassPath

>BootStrapClassLoader is responsible to load classes from BootStrapClassPath

>BootStrapClassLoader is by default availble with jvm.

2. ExtensionClassLoader: [child of BootStrapClassLoader]


------------------------
>responsible to load classes from Extension class path
ex: jdk\jre\lib\ext\ _.class

3. ApplicationClassLoader: / SystemClassLoader: [it use environment var]


----------------------------------------------
> its child of ExtensionClassLoader
> responsible to load classes from Application Class Path (current working
directory)

----------------------------------------------------------------------------------

Q- HOW CLASSLOADER WORKS ?


ANS:
> it follows Delegation Hierarchy principle.
Step 1: whenever jvm comes/start to load a perticular class,
first it will check that corresponding class is already loaded or not.
if it is already loaded in Method Area then jvm use that loaded class.
if its not loaded then

Step 2: JVM gives/Handover this req to ApplicationClassLoader

Step 3: ApplicationClassLoader give this req to ExtensionClassLoader and then

Step 4: ExtensionClassLoader give this req to BootStrapClassLoader.

BootStrapClassLoader search in BootStrapClasspath for req .class file


ex: jdk\jre\lib
if req .class availble , then it will load
else
BootStrapClassLoader give this req to ExtensionClassLoader

Step 5: ExtensionClassLoader will search in ExtensionClass path


ex: jdk\jre\lib\ext.
if req .class avaible then load
else ExtensionClassLoader give this req to BootStrapClassLoader

Step 6: ApplicationClassLoader will search in ApplicationClass path /(current


working directory)
if availble then load
else we get errore->ClassNotFoundError or NoClassDefFound errore

Note:-if class availbe in both extension and bootstrap


then ClassLoader subsystem will give Highest priority for Bootstrap class
path.
-----------------------------------------------------------------------------------
-

Q-WHAT IS CUSTOMIZED CLASSLOADER ?


----------------------------------
>prob: Default classloader will load file only once eventhough we r using multiple
times that class in our prog
>after loading .class file if it is modified outside, Then
"Default ClassLoader" will not load updated version of .class file dynamically.

Solved: by using Customized ClassLoader:


-------------------------------
>defing our own Customized ClassLoader we can control class loading and
we can load class file separately every time. so updated version will be availble
every time.

Q-how to define OWN ClassLoader ?


--------------------------------
ans:- by extending Java.langClassLoaderClass

Q-what is Java.langClassLoaderClass?
-------------------------------------
>Java.langClassLoaderClass - acts as Base class for desining our own customized
classloader.
>every customized classloader should extends -Java.langClassLoaderClass. directally
or indirectally.

----------------------------------------------------------------

****** Various Memory Area of JVM:


>loading and running a java program , jvm req memory to store several things
like - ex: Byte Code, objects, variable,

Note: JVM MEMORY ORGANIZED IN 5 CATEGORY:


------------------------------------------
1. METHOD AREA
2. HEAP AREA
3. JAVA STACK AREA
4. PC REGISTER AREA
5. NATIVE METHOD STACK AREA

1. METHOD AREA: [total class level binary information including static var stored
in method area]
---------------
>created at the time of jvm start-up
>method area will be shared by all threads(global memory)
>method area shows runtime constant pool.
>total class level binary information including static var stored in method area

2. HEAP AREA: [prog view it considerd imp memory area, all (instance var's + obj
ref + object data) will be stored here]
-------------
>created at jvm start-up.
>heap area can be accessed by all threads or (its also shareable memory)
>note: array in java, also is an object so will be stored in Heap Memory only.
>all obj data will be stored in heap area.

Q- HOW TO CHECK HEAP AREA MEMORY STATISTICS BY PROGRAMMER ?


-----------------------------------------------------------
ANS: our java prog or app can communicate with JVM by using -"Runtime" class/object
present in java.lang package.
> Runtime class is a singleton class
>we can create runtime class obj by
ex : Runtime r = Runtime.getRuntime();

Note: after getting Runtime obj we can call following methods.

1 maxMemory(); -> return No of Bytes of max memory allocated to heap


2 totalMemory(); -> return no of bytes of total memory allocated to heap.
3 freeMemory(); -> return no of Bytes of free memory present in heap.

Ex:
class HeapMemory
{

public static void main(String[] args)


{
Runtime r = Runtime.getRuntime();

System.out.println("Total no of BYTE mem allocated to heap :


"+r.totalMemory());
System.out.println("total mem in MB :"+r.totalMemory()/1021);

System.out.println("Max allocated in MB :"+r.maxMemory()/1024);


System.out.println("FREE mem in MB :"+r.freeMemory()/1024);
System.out.println("-----------------------------------------------");
System.out.println("total allocated mem in GB
:"+(r.totalMemory()/1021)/1024);
System.out.println("Max allocated in GB :"+(r.maxMemory()/1021)/1024);
System.out.println("FREE in GB :"+(r.freeMemory()/1021)/1024);
}
}

------------------------------------------------------------

Q - HOW TO SET MAXIMUM AND MINIMUM HEAP SIZE ?


----------------------------------------------
>Heap memory size based on our req we can Increase or Decrease heap size
Note: default heap size is 64 mb;

-Xmx: USED TO SET MAXIMUM EX-128:


----------------
EX: java -Xmx128m HeapDemo

-Xms: USED TO SET MINIMUM EX-64:


----------------
EX: java -Xms64 HeapDemo

-------------------------------------------------

3. JAVA STACK AREA: [all method calls+local var + results will be stored in
stack]
-------------------
>For every Thread-> JVM will create a seperate Runtime stack. and Runtime Stack
will be created Automatic at
the time of Thread creation.
>all method calls+local var + results will be stored in stack
>For every Method call -> a seperate entry will be added to stack , this entry
called 'stack frame'
and after completion method stack will be romoved.
>in last just before terminating thread, runtime stack will be destroyed by JVM.

STACK FRAM: [it consist of local var(int,float,double,ref), method results,]


-----------

Structure of stack frame: [consist in 3 parts]


------------------------
1. Local var Array:
2. Operand stack:
3. Frame Data:

1. Local var Array: [it contains slot/entry , and each slot/entry is 4 byte]
-------------------
>it contains all parameters and local var of method,
value of int, float, and ref var occupy 1 entry / 1 slot in that array,
Note:- long and double occupy 2 slot / 2 entry.
Note:- byte, short, char values will be converted to int before storing in slot and
occupy 1 sloat.
Note:- most of jvm follow 1 slotn for boolean value but its varied from jvm to jvm.

2. Operand stack:
-----------------
> operand stack is a work space for jvm,

3. Fram Data:
-------------
>all symbolic references (constant pool) related to that method.
>it contains ref to Exception table which provide correspondence catch block info
in case of exception.

4. PC (programm counter) Register Area:


---------------------------------------
>pc register contains address of current executing instruction.
>for every thread a seperate pc register will be created at the time of thread
creation.
and after completion of execution pc register will be incremented to hold to
address of next instruction.

5. NATIVE METHOD STACK AREA:


----------------------------
>for every Thread - jvm will create a seperate Native Method Stack.
all native method calls invoked by Thread will be stored in correspondence native
method stack.

Imp Note:
1-> Method area, Heap area, are considerd as major memory area with respect to
programmer point of view.
2-> Method area, Heap area, are for JVM,
Whereas
Stack Area, PC Register area and Native Method Area are used for Thread.

->one seperate Heap Area for Every Jvm


->one seperate Method Area for Every Jvm
and
->one seperate Stack for Every Thread
->one seperate PC Register for Every Thread
->one seperate Native Method Stack Area for Every Thread
Note:- > static variable will be stored in Method area,
where as
instance variable will be stored in Heap area
and
Local variable will be stored in stack area.

----------------------------------------------------------------------------

******EXECUTION ENGINE:
-----------------------
>its central component of JVM, responsible to execute java class files

it contains 2 component to execute java classes.


1. Interpreter
2. JIT compiler

1. Interpreter: [Convert Line by Line]


---------------
>its responsible to
1. Read Byte code
2. Convert Byte Code into Machine code / (Native code) line by line.

Problum: - > it interprets every time even the same method even multiple times.
which reduce Perform of system.
Solved:- > introduced JIT compiler in 1.1 version.

2. JIT compiler:
----------------
>main perpose is to improve performance, because internally jit maintaine a seprate
count for every method.
>always First method will be interpreted normally by interpreter jit increment the
count var.
this process will be continued for every method.
>if any method comes to Threshold value (starting point for a new thread)
then jit identify that method repeately used , immmediataly jit compiles that
method and generate Native code
Next time jvm come across that method call then jvm direct use Native code
means comiler will not compile again this so performance will be improved.

Note: Profiler :- which is part of compiler is responsible to identify HOTSPOT

Note:- JVM interprets Total program line by line at least once.


JIT compilation is applicable only for repeatedly invoked methods. but not
for every time.

** JNI-[JAVA NATIVE INTERFACE]:


------------------------------
>JNI acts as mediator between java method calls and corresponding native libraries.
ex: hashCode();
Note-> see final diagram durga volume 3 page 179

===================================================================================
============

You might also like