Friday, 22 April 2016

JVM architecture


JVM Architecture

To execute any code, JVM utilizes different components. JVM is divided into several components :
The Stack
Stack in stores various method arguements 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. (vars, frame, optop)

There are 3 sections in Java stack frame 
1. Local variables
contains all the local variables being used by the current method invocation. It is pointed to by the vars register.

2. Execution environment
This section is used to maintain the operations of the stack itself. It is pointed to by the frame register.

3. Operand stack
used as a work space by bytecode instructions.
It is here that the parameters for bytecode instructions are placed, and results of bytecode instructions are found.
The top of the operand stack is pointed to by the optop register.


Method area
This is the area where bytecodes reside.

The program counter 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.
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.


Heap (Garbage-collected Heap)
It is 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.
Note : 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.

Heap is divided into 3 parts :
 1. New / Young generation
      New Generation is further divided into 3 parts :
        a. Eden space
        b. Survivor1 space
        c. Survivor2 space
        When an object first created in heap its gets created in new generation inside Eden space and after subsequent Minor Garbage collection if object survives its gets moved to survivor 1 and then Survivor 2 before Major Garbage collection moved that object to Older tenured generation.

 2. Tenured / Old Generation

 3. PermGen (Perm area) of heap
Permanent generation of Heap or Perm Area of Heap is somewhat special and it is used to store Metadata related to classes and method in JVM.
It also hosts String pool provided by JVM.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.