Page Contents
The JVM (Java Virtual Machine) is an abstract machine. This is a specification that provides a runtime environment that can execute the Java bytecode. It is platform dependent.
JVMs are available for number of hardware and software platforms and
JVM uses as :
- A specification that specifies the operation of the Java virtual machine. But the distribution provider is independent in choosing the algorithm. Its implementation was provided by Oracle and other companies.
- An implementation Its implementation is provided by JRE (Java Runtime Environment).
- Runtime Instance Whenever you run the program using command prompt , and instance of JVM is created.
JVM performs :
- Loads code
- Verifies code
- Executes code
- Provides runtime environment
JVM also provides definitions for the:
- Memory area
- Class file format
- Register set
- Garbage-collected heap
- Fatal error reporting etc.
JVM Architecture
We understand the internal architecture of the JVM. It has a class loader, memory area, runtime, etc.
Classloader
Classloader is a JVM subsystem that is used to load class files. Every time we run the Java program, it is first loaded by the class loader. There are three class loaders built into Java.
- Bootstrap ClassLoader is the first class loader which is the superclass of Extension Classloader. Load the rt.jar file that contains all the Java Standard Edition class files such as java.lang package classes, java.net package classes, java.util package classes, java.io package classes, java.sql class package etc.
- Extension ClassLoader is the parent class loader of the system class loader and child class loader of Bootstrap. Load the jar files located in the $ JAVA_HOME / jre / lib / ext directory.
- System/Application ClassLoader is a child class loader of the extension class loader. Load class files from classpath. By default, classpath is set to the current directory. You can change classpath using “-cp” or “-classpath” options. Also known as an application class loader.
ClassLoader Java Example
import java.util.HashMap; public class Demo { public static void main(String[] args) { System.out.println("class loader for Demo class" + Demo.class.getClassLoader()); System.out.println("class loader for HashMap: " + new HashMap().getClass().getClassLoader()); } }
Output:
class loader for Demo classsun.misc.Launcher$AppClassLoader@73d16e93
class loader for HashMap: null
In above program are the internal classloaders provided by Java. If you want to create your own classloader, you need to extend the ClassLoader class.
Class(Method) Area
In this area, all the class-level information are stored such as the class name, parent class name, variable data and methods, etc., including static variables. There is only one method area in JVM and this is a shared resource.
Heap
The heap area is the runtime data area in which all objects are stored. There is also a dynamic storage area for JVM. It is also a shared resource.
Stack
The JVM creates a runtime stack that is stored here for each thread.Each thread has a private JVM stack, created at the same time as thread.
Each block in this stack is called a record frame / activation stack which stores method calls. All local variables of the method are stored in the appropriate frame. At the end of the thread, the runtime stack will be destroyed by the JVM. This is not a shared resource.
Program Counter Register
The program counter register contains the address of the currently running Java virtual machine instruction.
Native Method Stack
It consist the native methods used in the application.
Execution Engine
It consist:
- A virtual processor
- Interpreter that read bytecode stream then execute the instructions.
- Just-In-Time(JIT) compiler is used to improve performance. JIT compiles parts of the bytecode that have similar functionality at the same time and therefore reduces the time required for compilation. Here, the term “compiler” refers to a translator from the instruction set of a Java virtual machine (JVM) to the instruction set of a specific CPU.
Java Native Interface
Java Native Interface (JNI) is a framework that provides an interface to communicate with another application written in another language like C, C ++. Java uses the JNI framework to send results to the console and interact with the operating system libraries.