The Java Virtual Machine (JVM) is a virtual or abstract machine developed to execute the java programs. A Java Virtual Machine is an implementation of the Java Virtual Machine Specification which executes the Java program (byte code). This Java Virtual Machine Specification is implemented by different companies to develop the JVM.
Note: Java Virtual Machine (JVM) can be used to execute the java programs but not for compiling the java programs.
What is Java Virtual Machine Specification?
The Java Virtual Machine Specification is a document that formally describes or provides some rules for implementation of the Java Virtual Machine. For JVM there is a single Java Virtual Machine Specification which ensures that all implementations are interoperable or relevant.
Let’s understand the internal architecture of Java JVM; which every java programmer must know.
The class loader subsystem is an important part of the Java Virtual Machine which loads the java class files for execution.
The class loader subsystem dynamically performs the activities like loading, linking and initializing classes and interfaces:
I. Loading:-
Loading is the process of finding the binary representation of a class or interface type with a particular name and creating a class or interface from that binary representation.
II. Linking:-
Linking is the process of taking a class or interface and combining it into the run-time state of the Java Virtual Machine so that it can be executed.
III. Initializing:-
Initialization of a class or interface consists of executing the class or interface initialization method < clinit >
Types of Class Loader Subsystem
The Java virtual machine contains two kinds of class loaders: a bootstrap class loader and user-defined class loaders.
I. Bootstrap Class Loader Subsystem:-
Every Java virtual machine implementation has a bootstrap class loader subsystem, which knows how to load trusted classes, including the classes of the Java API. The Java virtual machine specification doesn't define how the bootstrap loader should locate classes.
II. User Defined Class Loader Subsystem:-
In a JVM, each and every class is loaded by some instance of a java.lang.ClassLoader. The ClassLoader class is located in the java.lang package and developers are free to subclass it to add their own functionality to class loading.
The Runtime Data Area is divided into 5 major components:
I. Method Area – All the class level data will be stored here, including static variables. There is only one method area per JVM, and it is a shared resource.
II. Heap Area – All the Objects and their corresponding instance variables and arrays will be stored here. There is also one Heap Area per JVM. Since the Method and Heap areas share memory for multiple threads, the data stored is not thread safe.
III. Stack Area – For every thread, a separate runtime stack will be created. For every method call, one entry will be made in the stack memory which is called as Stack Frame. All local variables will be created in the stack memory. The stack area is thread safe since it is not a shared resource. The Stack Frame is divided into three sub entities:
IV. PC Registers – Each thread will have separate PC Registers, to hold the address of current executing instruction once the instruction is executed the PC register will be updated with the next instruction.
V. Native Method stacks – Native Method Stack holds native method information. For every thread, a separate native method stack will be created.
The byte code which is assigned to the Runtime Data Area will be executed by the Execution Engine. The Execution Engine reads the byte code and executes it piece by piece.
JNI will be interacting with the Native Method Libraries and provides the Native Libraries required for the Execution Engine.
It is a collection of the Native Libraries which is required for the Execution Engine.
Different types of Java Virtual Machine (JVM) Implementation:
1. HotSpot- the primary reference Java VM implementation provided by Oracle Corporation which comes as part of JDK and JRE.
2. OpenJDK (Open Java Development Kit) is a free and open source implementation of the Java Platform, Standard Edition (Java SE).
There are many more JVMs available, check the list of JVMs.