Java Virtual Machine or JVM as its name suggest is a “virtual” computer that resides in the “real” computer as a software process. JVM gives Java the flexibility of platform independence. Let us see first how exactly Java program is created, compiled and executed.
Java Virtual Machine or JVM as its name suggest is a “virtual” computer that resides in the “real” computer as a software process. JVM gives Java the flexibility of platform independence. Let us see first how exactly Java program is created, compiled and executed.
Java code is written in .java file. This code contains one or more Java language attributes like Classes, Methods, Variable, and Objects etc. Javac is used to compile this code and to generate .class file. Class file is also known as “byte code“. The name byte code is given may be because of the structure of the instruction set of Java program. We will see more about the instruction set later in this tutorial. Java byte code is an input to Java Virtual Machine. JVM read this code and interpret it and executes the program.
Java Virtual Machine like its real counter-part executes the program and generates output. To execute any code, JVM utilizes different components.
JVM is divided into several components like the stack, the garbage-collected heap, the registers and the method area. Let us see diagram representation of JVM.
Stack in Java virtual machine stores various method arguments as well as the local variables of any method. Stack also keeps track of each and every method invocation. This is called Stack Frame. There are three registers that help in stack manipulation. They are vars, frame, and optop. This registers points to different parts of current Stack.
There are three sections in Java stack frame:
Local Variables :
The local variables section contains all the local variables being used by the current method invocation. It is pointed to by the vars register.
Execution Environment
The execution environment section is used to maintain the operations of the stack itself. It is pointed to by the frame register.
Operand Stack
The operand stack is used as a work space by byte code instructions. It is here that the parameters for byte code instructions are placed, and results of byte code instructions are found. The top of the operand stack is pointed to by the optop register.
This is the area where byte codes reside. The program counter points to some byte in the method area. It always keeps 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 than one thread is accessing any specific method or any instructions, synchronization is needed. Synchronization in JVM is achieved through Monitors.
The Garbage-collected Heap 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. Unlike C++, Java does not have free operator to free any previously allocated memory. Java does this automatically using Garbage collection mechanism. Till Java 6.0, mark and sweep algorithm is used as garbage collection logic. Remember that 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.
No comments:
Post a Comment