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

View Comments

  • 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 ///

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

  • fantastic article.......................i haven't read such awesome article before this............quite remarkable............

  • 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?

    • 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?

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

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

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

  • 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 !

Recent Posts

  • Java

Java URL Encoder/Decoder Example

Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…

5 years ago
  • General

How to Show Multiple Examples in OpenAPI Spec

Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…

5 years ago
  • General

How to Run Local WordPress using Docker

Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…

5 years ago
  • Java

Create and Validate JWT Token in Java using JJWT

1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…

5 years ago
  • Spring Boot

Spring Boot GraphQL Subscription Realtime API

GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…

5 years ago
  • Spring Boot

Spring Boot DynamoDB Integration Test using Testcontainers

1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…

5 years ago