Thursday 6 February 2014

Java Virtual Machine (JVM) Architecture / Java Memory Structure

Java Virtual Machine (JVM) is the heart of entire Java program execution process. 

JVM has 3 major modules:

1. Class Loader subsystem module
2. Run time data areas
3. Execution Engine

Java compiler at the time of compilation converts the .java program in to a .class file which consists of byte code instructions. Remember, the  Java compiler is part of Java Development Kit (JDK)  and not of JVM.

JVM loads and interprets the .class file which is generated by Java Compiler. The .class files are loaded in to JVM by a module of JVM  called  Class Loader Sub System.

Class Loading mechanism will be discussed in more details in other post.

Class loader performs following actions :--

1. First, it loads the .class files in memory.
2. Then  it verifies if all the byte codes instructions are proper or not. If any instruction is found suspicious then execution is terminated immediately.
3. If all the byte code instructions are proper, then necessary memory to execute the program is allocated.

JVM divides the memory in to 5 parts, called  run time data areas which contains the data and results while running the program. Please refer the following figure :--


1. Method Area : Method area is the memory block which stores the class code, variables code and code of the functions in Java program.

2. Heap: Heap is the memory area where object are created when program executes.

Note :-- Method areas and Heap areas are created immediately whenever class loader loads the .class file in memory.

3. Java Stacks : As method code / function code in Java program is stored on method area, but when a method is executed, it needs some more memory to the data and results which is allotted on Java Stacks. Thus java stacks are memory area where methods  are exectued.

4. PC(Program Counter) Registers: These are the memory areas which stores the memory address of the instructions of the methods. If there are 5 methods, 5 PC registers are used to track the instructions of the method.

5. Native Method stacks : As Java methods are executed on Java stacks memory area, native methods (C/C++ functions)  are executed on native method stacks.  Note : Java can execute native methods written in C/ C++ through native method  interface.


Execution Engine Module:

It contains the interpreter and JIT (Just in Time) compiler which translates the byte code instructions from .class files in to machine instructions which are executed by the microprocessor.  Loops and iterations (hot areas) in .class files are executed by JIT compiler whereas normal statements and instructions of the Java program are executed by Java interpreter. 

 

   



    


 

No comments:

Post a Comment