Tuesday 23 August 2016

JVM Architecture:

JVM - Java Virtual Machine is used to interpret the .class file which is in byte code format to machine understandable format.

Machine understandable format is 0's and 1's. JVM architecture is given in the below diagram and its very important to understand the architecture.




JVM resides on the RAM of the machine which takes care of execution of the statement memory creation, object creation and releasing memory. This makes developers life very easy.

Class Files:

Java file which you write after compilation generated .class file. The .class file is a byte code which will not be in readable format. The class file is given to the JVM for interpreting it to machine understandable format.

Class Loader Subsystem:

Class loader loads the class files and if there is any issues in that file it will be prevented from further execution. This is the main reason for Java being secure. If there are any virus defined files this will not be allowed for execution by which virus is prevented. 

Method Area:

Java class contains methods and variables. Memory for methods and variables are allocated in the method area. 

Heap: 

Heap holds the memory for the object of the class.

Stack:

While executing the java class it needs memory for storing the intermediate results and intermediate processing. Java Stack is used for storing the intermediate values.

PC register:

These are teh registers which contains memory address of the instructions of the methods.

Native method stack:

Native methods are executed in this location.

Execution Engine = Interpreter and JIT


JIT - Just In Time Compiler.

Interpreter interprets the byte code and converts it into machine understandable format. This will arise a question why JIT is required.

JIT is used for processing the bulk data. Below is the clear example for this,

int a =10,b=13;
System.out.println(a);
System.out.println(b);

for(i=1;i<=a;i++)
{
System.out.println(a);
}


In the above example 'a' will be printed after interpreting by the interpreter which takes 2 nanoseconds. Similarly second statement 'b' is printed in 2 nano seconds.

In same manner for loop which prints same value 'a' in a loop for 10 times. This will take 2 X 10 nano seconds = 20 nanoseconds. This type of task is given to the JIT compiler. JIT will take 2 nanoseconds to print 'a' first time and then creates memory for next 9 iterations and pushes the same value to the memory, this will take another 2 nanoseconds. Totally 4 nanoseconds will be taken which reduces much of the time.

JVM decides which statement should be given to JIT and interpreter.



1 comment:

Prabhuraj said...

What is the use of native libraries