JVM : Java Virtual Machine

The JVM is the specification for a software program that executes code and provides the runtime environment for that code. The JVM manages system memory and provides a portable execution environment for Java-based applications.

It is:

A specification where working of Java Virtual Machine is specified. But implementation provider is independent to choose the algorithm.

An implementation is a computer program that meets the requirements of the JVM specification

Runtime Instance Whenever you write java command on the command prompt to run the java class, an instance of JVM is created.


Here is how JVM works

First, Java code is complied into bytecode. This bytecode gets interpreted on different machines Between host system and Java source, Bytecode is an intermediary language.
JVM in Java is responsible for allocating memory space.


Working of JVM
Working of Java Virtual Machine (JVM)

Java Virtual machine(JVM) is a very important part of both JDK and JRE because it is contained or inbuilt in both. Whatever Java program you run using JRE or JDK goes into JVM and JVM is responsible for executing the java program line by line hence it is also known as interpreter.


JVM Architecture

Let's have a quick overview of the JVM architecture. JVM architecture in Java contains classloader, memory area, execution engine etc.

JVM architecture
Java Virtual Machine Architecture

ClassLoader
The class loader is a subsystem used for loading class files. It performs three major functions viz. Loading, Linking, and Initialization.


Method Area
Method Area stores class structures like metadata, the constant runtime pool, and the code for methods.


Heap
All the Objects, their related instance variables, and arrays are stored in the heap. This memory is common and shared across multiple threads.


JVM language Stacks
Java language Stacks store local variables, and it’s partial results.


PC Registers
PC register store the address of the Java virtual machine instruction which is currently executing.


Native Method Stacks
Native method stacks hold the instruction of native code depends on the native library.


Execution Engine
It is a type of software used to test hardware, software, or complete systems.


Native Method interface
It allows Java code which is running in a JVM to call by libraries and native applications.


Native Method Libraries
Native Libraries is a collection of the Native Libraries(C, C++) which are needed by the Execution Engine.