Tuesday, November 10, 2015

How does JVM works internally in java ?

JVM :  

JVM (Java Virtual Machine) is an abstract machine. It is a specification that provides runtime environment in which java bytecode can be executed. JVM was designed as an interpreter for byte code. JVM is the heart of entire Java Program Execution process. The fact that java a program is executed by the JVM also helps to make it secure.Translating a Java Program into byte code makes it much easier to run in a wide variety of environments. In general, when a program is compiled to an intermediate form and then interpreted by a virtual machine,it runs slower than it would run if compiled to executable code.

HotSpot provides a Just-In-Compiler(JIT) for byte code to boost performance of execution(to covert byte code to native code). JIT is also part of the JVM.  





What it Does :
                             
JVM perform the fallowing opertion: 

1.Loads the code
2.Virify the code
3.Execute the code
4.Provide the runtime Environment      

When we write a Java Program we need to compile it with a command like javac  filename.java this .java file will generate java class file(.class). 

The fallowing steps will understand what exactly happening internally by JVM

Step 1: javac Test.java then it will generate java class file(.class file) by compiling java file using javac  . So, java compiler is responsible to generate .class file   

Step 2: This .class file acts as import to class loader to load and execute .class file

Step 3: This class loader is responsible to take input as .class file to load and execute. The class loader again contains three parts loading,linking and initialization.All core java API classes are going to take care by Boot Strap class loader.

Step 4: After loading .class file verification of the byte code will check whether your byte code properly generated or not. If verification fails then verify error throws. So, we can say that java provides full security programming language.

Step 5: After initialization class loading is successfully by class loader. Here initialization means static variables original values will be assigned and static blocks will be executed.

Step 6: After that,inside JVM there are three types of are there,they are method area,Heap area,stack area,PC register. In method area class level data and static variables will be exist,and inside Heap area Object data and corresponding instance variables will be exist,for local variables take care by stack area.

Step 7: After .class file loaded by class loader then execution engine is responsibility to execute .class file

Step 8: Finally we get result by providing java filename command.


Internal Architecture of jvm : 

 


Let's understand the internal architecture of jvm .

It contains class loader , memory area , execution engine 

1)  class loader :

class loader is a subsystem of JVM that is used to load class files. It loads the .class file into memory.Then it verifies whether all byte code instructions are proper or not. If it finds any suspicious,then execution is rejected immediately. If instructions are proper then it allocates necessary memory to execute the program.

2) class area:     

class area is also called method area. It is a memory block,which stores the class code,code of the variables and code of the methods in the java program.
3) Heap :  

It is the runtime data area in which objects are allocated that means where objects are created. Whenever JVM loads a class,a method and a heap area are immediately created in it.

4) stack : 

Java Stack stores frames.It holds local variables and partial results, and plays a part in method invocation and return.Each thread has a private JVM stack, created at the same time as thread.A new frame is created each time a method is invoked. A frame is destroyed when its method invocation completes.

5) Program Counter :  

PC (program counter) register. It contains the address of the Java virtual machine instruction currently being executed. In other words, PC contains memory address of the instructions of the methods.

6)Native method stack

It contains all the native methods used in the application. Native methods are nothing but C/C++ functions. To execute native methods,generally native method libraries are required. These header files are located and connected to JVM by a program called Native method interface.

7) Execution Engine

Execution Engine Contains interpreter and JIT compiler

Interpreter It is used to converting the byte code instructions into machine code so that the processor will execute them. Most of the JVM implementations use both the interpreter and JIT compiler simultaneously to covert byte code .

Just-In-Time(JIT) compiler:It is used to improve the performance.JIT compiles parts of the byte code that have similar functionality at the same time, and hence reduces the amount of time needed for compilation.Here the term ?compiler? refers to a translator from the instruction set of a Java virtual machine (JVM) to the instruction set of a specific CPU.



No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...