Monday, 28 May 2012

Java : JVM Inside. The Story behind JVM. Continue......

In , the previous post , we have discussed some points about JVM in Java platform. Including JVM, java has its own Run time environment(JRE). There are many versions available for JRE, download here.Being byte codes are binary format, Programs intended to run on a JVM must be compiled into a standardized portable binary format, which typically comes in the form of .class files. A program may consist of many classes in different files. For easier distribution of large programs, multiple class files may be packaged together in a .jar (Java Archive file ) file.

The Java application launcher, java, offers a standard way of executing Java code. The JVM runtime executes .class or .jar files, emulating the JVM instruction set by interpreting it, or using a just-in-time compiler (JIT) such as Oracle's HotSpot. JIT compiling, not interpreting, is used in most JVMs today to achieve greater speed. There are also ahead-of-time compilers that enable developers to precompile class files into native code for particular platforms.

Almost virtual machines are similar and the Java virtual machine has a stack-based architecture akin to a microcontroller/microprocessor. However, the JVM also has low-level support for Java-like classes and methods.


JVM have following inside paradigm :- 

1. Bytecode verifier
2. Stack
3. Garbage Collected Heap
4. Method Area


Bytecode Verifier :

It helps to jvm to verifies all bytecode before it is executed. This verification consists basic of three types of checks:-
       1. Branches are always to valid locations
       2. Data is always initialized and references are always type-safe
       3. Access to private or package private data and methods is rigidly controlled.

Stack: 

Stack in Java virtual machine stores various method arguments as well as the local variables of any method. Stack also keep track of each an every method invocation. This is called Stack Frame. There are three registers thats help in stack manipulation. They are vars ( local variable), frame (Execution environment), optop ( Operand Stack ). This registers points to different parts of current Stack.


Method Area:

The byte codes are here. The program counter (PC) points to some byte in the method area. It always keep tracks of the current instruction which is being executed (interpreted). After execution of an instruction, the JVM sets the PC to next instruction ( As we know in ASM language). Method area is shared among all the threads of a process. Hence if more then one threads are accessing any specific method or any instructions, synchorization is needed. Synchronization in JVM is acheived through Monitors (Read this post).

Garbage Collected Heap:

This is one of the main part of java/jvm .This is the place where the objects in Java programs are stored. Whenever we allocate an object using new operator, the heap comes into picture and memory is allocated from there. Unlike C++, Java does not have free operator to free any previously allocated memory. Java does this automatically using Garbage collection mechanism.The garbage collection logic for make memory free and available the resource for more development.

Point to Remember: The local object reference resides on Stack but the actual object resides in Heap only. Also, arrays in Java are objects, hence they also resides in Garbage-collected Heap.