A Java Virtual Machine (JVM) is an abstract computing machine that enables a computer to run a Java program.
Purpose of JVM:
The purpose of JVM is to provide platform-independent programming environment that abstracts away details of the underlying hardware or operating system, and allows the program execution in the same way on any platform.
JVM is responsible for converting .class file’s each byte code instruction into the machine language instruction that can be executed by the microprocessor.
Architecture of JVM:
To start with, the .java program is converted in to a .class file that consists of byte code instructions by the Java Compiler. Now this .class file is given to the JVM.
Class Loader Sub System
First, the Class Loader Sub System loads the .class file into memory, and then it verifies whether all byte code instructions are proper and allocates necessary memory to execute the code.
This is the area where JVM stores the class code, code of the variables and code of the methods written in Java Program.
This is the area, where java objects are created. Note: Whenever JVM loads the .class file, a method and a heap area are created immediately in the JVM.
Now we know that method code is stored in the Method area. But while running a method JVM needs some more memory in order to store the data and results. This memory is allotted on Java Stacks. So we can say Java Stacks are the memory areas where java methods are executed.
PC (Program Counter) Registers:
PC Registers are the memory areas, which contains the memory addresses of the instructions of the methods. Let’s say if there are two methods written inside the class, then two PC registers will be used to track the instructions of the methods.
Native Method Stacks:
We know the Java methods are executed on Java Stacks. Similarly Native methods like C/C++ functions are executed on Native Method Stacks.
Native Method Interface:
To execute Native methods, generally native method libraries (for example C/C++ header files) are required. These native interface programs are used to locate and connect the required native libraries to the JVM.
This block contains both Interpreter and JIT (Just in Time) compiler, which are responsible for converting byte code instructions into machine code. So that processor can execute them.
When the .class code is loaded in to the memory, JVM will decide which code needs to be executed by interpreter and which one to be executed by JIT compiler, so that the performance will be better. JIT compiler increases the speed of execution. The blocks of code which are allotted to JIT compiler are called hotspots.
Thus both interpreter and JIT compiler works together in JVM to translate the byte code instructions into machine code.