Java SE 8 and JDK 8 Launched.For Download:Click Here!


Social Icons


Friday, 31 January 2014

Internals of JVM

           We have learnt The Three Java BigShots - JDK,JVM,JRE. Also , we have seen the  Java Development Kit in (JDK) detail  . Now we will learn in depth about the Java Virtual Machine (JVM).  Java Virtual Machine is an abstract machine.
JVM is platform dependent execution environment that converts Java bytecode into machine laguage and executes it. 
Sun Microsystems has stated that there are over 5.5 billion JVM-enabled devices.

  •    All compilers translate source code into machine code for a specific platform.Java     Compiler produces an intermediate code known as bytecode.
  •    JVM is a specification that provides runtime environment in which java bytecode can be executed.
  •     It's a big part of the "Write once,run anywhere (WORA) " philosophy. (We had seen     WORA in our first post : Click here)
  •    JVMs are available for many hardware and software platforms (i.e.JVM is platform     dependent). For example, Sun Microsystems provides implementations of the JVM  for the Mac OS, Solaris OS and the Linux and the Microsoft Windows operating environments.

The following figure shows the main tasks performed by the JVM :

The Java Virtual Machine (JVM) is an abstraction layer between a Java application (i.e. the  program) and the underlying platform i.e. the operating system .


Now, we will discuss internal architecture of JVM . It contains classloader , class area ,Heap , Stack , program counter register , Native method stack and Execution engine .
Image Source :

1. Classloader :
    Classloader loads a java class file into java virtual machine.

2.Method Area :
   Method area is shared by all threads, created when the JVM starts. It stores runtime constant pool, field and method data , static variable , the code for methods . The method area can be implemented in various formats by JVM vendor. Oracle Hotspot JVM calls it Permanent Area or Permanent Generation (PermGen). The garbage collection for the method area is optional for each JVM vendor.
  (Note : When a method or field is referred to, the JVM searches the actual address of the  method or field on the memory by using the runtime constant pool.)

3.Heap :
   This area stores instances and objects and therefore it is the target of garbage collection (Garbage collection will be explained in further posts). JVM vendors can decide how to configure heap.

4.Stack :
    The stack is a Last in First Out (LIFO) data structure . Each thread has its own stack that holds a frame for each method executing on that thread. It is created at the same time as that of the thread. It holds local variables and partial results, and plays a part in method invocation and return . A new frame is created each time a method is invoked. A frame is destroyed when its method invocation completes.

5.Program Counter Register (PC Register ) :
   One PC (Program Counter) register exists for one thread, and is created when the thread starts. PC register has the address of a JVM instruction being executed now. If the current method is native then the PC is undefined.

6.Native Method Stack :
   It is a stack for native code written in a language other than Java. In other words, it is a stack used to execute C/C++ codes invoked through JNI (Java Native Interface). According to the language, a C stack or C++ stack is created.

7.Execution Engine :
   The bytecode assigned by the classloader to the JVM is executed by the execution engine .It is just like a CPU executing command one by one .
   It contains : 
  -  A virtual processor
  -  Interpreter reads, interprets and executes the instructions of the bytecode
             -  Just-In-TimeCompiler (JIT)  improves the performance  of the JVM . Bytecode having                   same functionality are compiled at the same time which in turn reduces the time needed                 for compilation.(To learn more about JIT :- Click Here )          

    So , we can think of the JVM as a hypothetical computer platform - a design that dosen't really
exist on any actual computer . The Java Runtime Environment(JVM) is an emulator that creates
a JVM environment that can execute programs. 

Keep Compiling !!

Comment With:
The Choice is Yours!


  1. "JVM is platform independent execution environment ... " - Since JVM is platform dependent, seems the mentioned statement is not correct. Because we have different JVM's (JRE's) for different Operating Systems, which executes machine independent java byte codes.

  2. You are absolutely right Ananda . That was a silly mistake done on our part.
    JVM is a platform dependent execution environment . First diagram on this post is also given above which explains what you are saying.
    Thanks Ananda !!

  3. Very good post admin. Specially Internal process defined step by step. For knowing more about Internals of JVM please CLICK HERE


Blogger Tips and TricksLatest Tips And TricksBlogger Tricks

Follow JavaCompile by Email

Related Posts Plugin for WordPress, Blogger...

Total Pageviews


Blogger Widgets