JVM Architecture

JVM Architecture

In this article, I am going to discuss JVM Architecture in detail. Please read our previous article where we discussed Java Packages with Examples. At the end of this article, I am sure you will understand the internal architecture of JVM.

JVM Architecture:

When we compile the Java program, first it will check whether the java program or code is valid or invalid. If the program is valid then the compiler will generate a .class file that contains byte code instructions. Byte code instructions are the special java instructions that are ready to convert into machine code instructions. These byte code instructions are executed by JVM (Java Virtual Machine).

JVM Architecture

ClassLoader Sub System

The ClassLoader subsystem is the first program available in JVM which is responsible for loading the .class file into JVM. Before loading the .class file into JVM it will check whether the .class file is containing valid byte code or not by using a program called “byte code verifier”. If the byte code is valid then it will load the .class file into JVM otherwise it won’t load the .class file.

Run-Time Data Areas

If byte code is valid the ClassLoader subsystem will load the .class file into the following five Run-Time Data Areas of JVM.

Method Area

Method Area contains all Class Code, Method Code immediate parent class name, methods, and variables information, etc. are stored, including static variables. There is only one method area per JVM, and it is a shared resource.

Heap

The Heap Memory area contains all the created objects. There is also one Heap Area per JVM. It is also a shared resource.

Java Stack

Java Stack contains all the methods which are under execution. Java Stack is a collection of different frames where each frame contains exactly one method.

Java Stack

PC (Program Counter) Register

PC (Program Counter) register contains the address of the next instruction to be executed. The value of the PC (Program counter) Register will be incremented automatically.

Native Java Stack

Native Java Stack contains all the native methods which are under execution.

Native Library

Native Library is a collection of all predefined non-java methods or native methods. The native method is a method that is written in non-java languages like C, C++.

Java Native Interface

Java Native Interface is responsible to load the required Native Methods into Java Native Stack. Sometimes we also use Java Native Interface for loading the required Native Methods into the Execution Engine.

Execution Engine

Execution Engine is responsible for converting byte code instructions into machine code instructions. Execution Engine contains the following two parts,

  1. Interpreter
  2. JIT Compilers

Here, both programs are used at the same time so that performance will be improved.

Interpreter

It interprets the bytecode line by line and then executes it. The disadvantage here is that when one method is called multiple times, every time interpretation is required.

JIT (Just In Time) Compiler

 It is used to increase the efficiency of the interpreter. It compiles the entire bytecode and changes it to native code so whenever the interpreter sees repeated method calls, JIT provides direct native code for that part so re-interpretation is not required, thus efficiency is improved.

Note: Code is executed by JIT compilers called “java hotspots”.

In the next article, I am going to discuss Java IO Streams in detail with examples. Here, in this article, I try to explain JVM Architecture in detail. I hope you enjoy this article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

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