Java Virtual Machine and its Architecture

Java Virtual Machine

Dan Dias Abeyesinghe
Javarevisited

--

The JVM can be simply known as the engine that provides a runtime environment for Java and its applications. The JVM is considered to be a part of the JRE (Java Runtime Environment). Its commonly and famously known for its process of converting java code to bytecode and then interpreting the bytecode into machine language.

However, the java compiler complies with a code to be used in the virtual machine which is also known as the Java Virtual Machine, unlike other programming languages where the compiler produces machine code for a particular system.

Java applications are also known as “write once run anywhere” as a Java code can be written in one system and is able to run in many Java-enabled systems without any changes made with the help of the JVM.

The Java JVM is platform-dependent (Mac JVM, Linux JVM, Windows JVM) whereas the Java code is platform independent.

JVM Architecture

The internal architecture of the JVM can be broken down into 3 main parts namely,

· The Classloader

· Memory Area

· Execution Engine

ClassLoader

This is considered as a subsystem of JVM and it is responsible for loading the class files when a program is run.

There are three inbuilt class loaders in Java namely,

· Bootstrap ClassLoader

· Extension ClassLoader

· System/Application ClassLoader

These perform three main functions.

Loading

The class loader reads the .class file and generates the required binary data which is stored in the method area. It also stores details like,

· the fully qualified name of the loaded class and its immediate parent class.

· If the generated .class file Is a Class, Interface or Enum

· Variables, modifiers, and method information.

After loading the .class file the JVM creates an object of type Class to represent this file in the heap.

Linking

This is where all the verifications, preparations, and resolutions are done. The verification process ensures the correctness of the file.

Preparation is the process of the JVM allocating memory for class variables and assigning default values in the memory.

Resolution is the process of converting symbolic references to direct references. This is done by going through the method area to locate the referenced entity. However, this function is optional

Initialization

Here all static variables and blocks are assigned with values mentioned in the code. This code is executed from top to bottom hierarchy.

JVM Memory

The JVM memory main consists of the following sectors,

· Method Area

· Heap

· Stacks

· PC Register

· Native Method Stack

Method Area

Here all the class level information is found (Class name, Class variables, immediate parent Class name etc.). It is important to note that there is only one method area per JVM and therefore it is considered to be a shared resource in the memory.

Heap

All the information related to Objects is stored here. This memory is also common and therefore shared among multiple threads.

Stacks

This store's local variables and the partial results. Each thread has its own runtime stack created when the thread is created. A new frame is created whenever a method is invoked and all the local variables of that method is stored in that corresponding frame. This is deleted when the method invocation is completed. This is not a shared resource .

PC Register

Also known as the program count register holds the current address of the JVM instruction currently being executed.

Native Method Stack

This holds all the native methods used in the application as implied by the name.

Execution Engine

This executes the .class file by reading the byte code line by line, using its data and information present in various memory areas, and executes instructions. It can be further broken down into 3 parts,

· Interpreter

This interprets the code line by line and executes, however, if one method is called multiple times it has the interpreted again and this can be considered as a disadvantage of it.

· Just-In-Time Compiler (JIT)

This is used as a remedy for the above problem where this increases the efficiency of the interpreter by compiling the entire byte code and changing it to native code, so when there is a repeat method call the JIT compiler will provide the native code for that part so that a re-interpretation is not needed.

· Garbage collector

This is used to destroy or get rid of un-referenced Objects.

Java Native Interface

JNI is a framework that provides an interface to communicate with other applications written in other languages. It allows Java code that is running in a JVM to call by libraries and native applications.

--

--