Java Virtual Machine, An inside story!!

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, 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-program-execution

Java Virtual Machine

Java Virtual Machine like its real counter part, executes the program and generate 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.

The Stack

Stack in Java virtual machine 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. They are vars, frame, 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 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. Synchronization in JVM is acheived through Monitors.

Garbage-collected Heap

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 a 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.

Update: Read this tutorial about Java Class File format.

Get our Articles via Email. Enter your email address.

You may also like...

14 Comments

  1. I find these articles very interesting and crisp in the way explanation is done. Its good to get back to basics and appreciate the technology behind our coding :) which we hardly do in our daily routine ///

  2. Andy says:

    Awesome Article .. one of best articles i came across in explaining the complexity of JVM so neatly… Kudos and happy blogging

  3. mandeep panghal says:

    fantastic article…………………..i haven’t read such awesome article before this…………quite remarkable…………

  4. vt says:

    I have a doubt…

    Java Virtual Machine (JVM) is interpreter But Just-in-time Compiler is the compiler.. Why both Interpreter and Compiler both are needed?
    What actions are done by JVM and what are done by JIT?

    • sriram says:

      Hi iam a newbie to java,even im also having the same doubt,what abt JIT.
      and what is the name of the java compiler?

    • Ganesh says:

      Hi….
      Java supports both compiler and interpreter…because of interpreter the program may be slow..(becse of line by line execution ).. JIT(Just in time compiler) is introduced for High performance..
      Ex:
      print a;//This is statement is executed by interpreter given by the JVM..for ex..the exec and
      printing this result may take 2 nano sec
      print b;//This is statement is executed by interpreter given by the JVM..for ex..the exec and
      printing this result may take 2 nano sec
      Repeat the following statements i from 1 to 10(like for loop)
      print a; //This is statement is executed by interpreter given by the JVM..for ex..the exec and
      printing this result may take 2 nano sec
      here the problem is if the above statement is hanld by interpreter the time taken by the staments (10*2=20 nano sec)
      ***So this type of statements(repeatitve stmnts) given to the JIT comipiler by JVM…******

      here 2 nano sec processing and separate memory is created so another 2 nano sec for storing..overll 4 nano sec for print a value (if i=1)..
      if i=2 the jvm fetch the rslt frm stored memry(The value is already availble)..
      here the time taken by JIT compiler is 4 nano sec….

  5. anamika says:

    really so imperssive article …………nd i also hv same dout about JIT.thanx 4 clearing the douts

  6. Mohamed says:

    It is really good, most important details are nicely described. thanks.

  7. shital says:

    interesting article…
    but is java virtual machine is platform independent??

  8. Sivaji says:

    Nice article

  9. Java Training in Chennai says:

    In Java, no object (variable) can ever hold an instance.
    A object (variable) can only hold a reference to an instance.

  10. Venkatesh says:

    Hi Viral Patel,

    You Tutorial is fantastic. You explained the entire story about JVM in a nutshell.
    I could understand without prior knowledge of JAVA.

    My best wishes to you !

  11. gsn says:

    hi viral,

    surely a gr8 article…thanks :)
    1 question though…would like to know where static variables, variables that are not local to any methods (like declared just within the class and not in any function) would be stored in JVM ??
    In C, these would be stored in Data Segment part of allocated space 4 a prog, but don’t know abt Java…

  12. Priya says:

    JVM is software that interprets Java bytecode and converts it into machine language to execute it on the computer. it is very useful information, thanks for sharing.

Leave a Reply

Your email address will not be published. Required fields are marked *