The JVM, or Java Virtual Machine, is an abstract machine that provides a runtime environment for executing Java bytecode. It is called a virtual machine because it does not physically exist. Instead, it is a specification that describes how a JVM should behave.
JVMs are available for a variety of hardware and software platforms, making them platform dependent. This means that the configuration of each operating system is different, and the JVM must be implemented accordingly. Despite this platform dependence, Java itself is platform independent, as it is designed to run on any platform that has a compatible JVM.
What is JVM?
The Java Virtual Machine (JVM) has three different aspects to its function:
- Specification: The JVM specification outlines how the Java Virtual Machine should function. The implementation provider is free to choose the algorithm to be used.
- Implementation: The implementation of the JVM is called the Java Runtime Environment (JRE). It is responsible for providing the runtime environment needed to execute Java programs.
- Runtime Instance: Every time a user runs a Java program from the command prompt using the “java” command, an instance of the JVM is created. This runtime instance executes the Java bytecode and provides a runtime environment for the program to run correctly.
What it does?
The JVM performs the following operation:
- Loads code
- Verifies code
- Executes code
- Provides runtime environment
JVM provides definitions for the:
- Memory area
- Class file format
- Register set
- Garbage-collected heap
- Fatal error reporting etc.
JVM Architecture
To understand how the JVM works internally, it’s essential to know its architecture.
1. Classloader
The Classloader is an essential component of the JVM that is responsible for loading class files into memory when a Java program runs. The Java Virtual Machine contains three built-in classloaders:
- Bootstrap Classloader: This classloader is the superclass of the Extension Classloader. It loads the rt.jar file that contains all the class files of Java Standard Edition, including java.lang, java.net, and java.util, java.io, and java.sql package classes.
- Extension Classloader: This classloader is the child of the Bootstrap Classloader and the parent of the System/Application Classloader. It loads the jar files that are located inside the $JAVA_HOME/jre/lib/ext directory.
- System/Application Classloader: This classloader is the child of the Extension Classloader. It loads the class files from the classpath. By default, the classpath is set to the current directory, but it can be changed using the “-cp” or “-classpath” switch. This classloader is also known as the Application Classloader.
//Let's see an example to print the classloader name public class ClassLoaderExample { public static void main(String[] args) { // Let's print the classloader name of current class. //Application/System classloader will load this class Class c=ClassLoaderExample.class; System.out.println(c.getClassLoader()); //If we print the classloader name of String, it will print null because it is an //in-built class which is found in rt.jar, so it is loaded by Bootstrap classloader System.out.println(String.class.getClassLoader()); } }
Output
sun.misc.Launcher$AppClassLoader@4e0e2f2a null
Java provides internal classloaders like Bootstrap ClassLoader, Extension ClassLoader, and System/Application ClassLoader. However, you can create your own classloader by extending the ClassLoader class.
2. Class(Method) Area:
The Class(Method) Area of JVM is used to store per-class structures such as the runtime constant pool, field and method data, and the code for methods.
3. Heap:
The Heap is the runtime data area where objects are allocated during runtime. The heap is shared among all Java threads and is created when the JVM starts up.
4. Stack:
The Java Stack is used to store frames. It holds partial results and local variables and plays a crucial role in method invocation and returns.
Each thread in Java has a private JVM stack that is created simultaneously with the thread. Whenever a method is invoked, a new frame is created. When the method invocation is complete, the frame is destroyed.
5. Program Counter Register:
The PC (program counter) register stores the memory address of the Java virtual machine instruction that is currently being executed.
6. Native Method Stack:
The Native Method Stack contains all the native methods that are used in the Java application.
7. Execution Engine:
The Execution Engine consists of a virtual processor, which interprets the bytecode stream and executes the instructions.
It also includes a Just-In-Time (JIT) compiler, which optimizes the performance of the Java application by compiling parts of the bytecode that have similar functionality at the same time. This reduces the amount of time needed for compilation.
8. Java Native Interface:
The Java Native Interface (JNI) is a framework that enables Java applications to communicate with applications written in other languages such as C, C++, and Assembly. The JNI framework is used in Java to interact with operating system libraries or to send output to the Console.
Follow tutorials.freshersnow.com for further learning on Java Virtual Machine.