Overview of Java Virtual Machine

Nikini Aloka
CodeX
Published in
4 min readJun 10, 2022

Do you Know JVM (Java Virtual Machine)? Let’s learn about JVM briefly.

Virtual Machine is dived into two parts. They are System based Virtual machines and Application based Virtual machines. Aplication-based Virtual machine means you don’t have any hardware device involved. But you may have software that helps to run the platform to another program. Application-based VM is called a process-based VM.

JVM is an application-based VM.JVM is nothing existing reality JVM is a complete specification it says how does should be done. When we download the JRE, JVM comes with JRE. It deploys all the codes which can create JVM. And also JVM takes care of byte code to machine code.

A JVM instance is created each time you run a Java application. It is in charge of the bytecode to machine code conversion process. It will create one JVM instance per Java program if you execute multiple Java programs.

So, when does a JVM instance get created? A non-daemon thread is created when a JVM instance is started. A JVM instance can also die in 2 Ways.

  1. If there are no non-daemon threads running
  2. 2. Application called system exit() method by itself.

Now we learn about the architecture of JVM. It has 3 components inside the JVM . They are

  1. class Loader
  2. Runtime Memory/Data Areas
  3. Execution Engine.
JVM Architecture

Now we can know these 3 components one by one.

1. class Loader-

  • ClassLoader performs 3 main tasks. They are Loading, Linking, and Initialization. Now we can identify them one by one.

Loading

Before the loading process, the class loader reads the following things of a class that is supposed to be loaded. Fully qualified name, variable information, Immediate parent, Checks whether it is a class, interface, or an enum. After reading the above items, the class is loaded to the memory area. There are 3 Class Loaders like below.

Linking

This process can be divided into three main parts.

  1. Verification-

VM can determine whether or not it is safe to execute. To do this, the JVM features a bytecode verifier. It examines the compiler, format, and structure. If any of these are broken, the bytecode verifier will throw a verifier exception. Otherwise, if it is a pass, the next phase is preparation.

2. Preparation-

In this phase, For all static variables memory will be allocated and assigned with default values based on the data types.

3. Resolution-

JVM replaces symbolic links with the direct links

Initialization

This process will assign real values to variables that were previously assigned default values in the Linking process’s preparation step. Furthermore, if any static blocks exist, they will be executed.
The JVM requires implementations to perform initializations before each class is utilized.

Active use of a class is,

1. Use a new keyword.

2. invoking a static method.

3. Assign a value to a static field.

4. if a class is an initial class (class with the main() method).

5. using a reflection API

6. initializing a subclass from the current class.

There are four ways to initialize a class: using the new keyword — this will take the class through the initialization process;
utilizing the clone() function — this gathers information from the parent object (source object).
This will go through the initialization process using the reflection API (getInstance();).
using IO.ObjectInputStream(); — this sets the initial value of all non-transient variables to the value of InputStream.

2. Runtime Memory /Data area-

This Memory area is divided into 5 categories. They are Method area, Heap, Java stack, Pc registers, and Negative Method stack. When we load the class,

  • The method area is stored all the class information(like type information).
  • All the objects come to the Heap area.
  • The stack area stores all local variables, method calls, and partial results of a program (not a native method). A runtime stack will be created for each thread. The “Stack Frame” is a stack area block that holds the local variables of method calls. As a result, after the method invocation is finished, the frame is removed (POP). Because this is a stack, the structure is Last-In-First-Out.
  • The pc register holds the information for the next execution.
  • When native methods are accessing this area, It holds native information/methods.

Method area and Heap are executed per VM. But the other 3 execute the per thread.

3. Execution Engine

The JVM’s execution engine is its most important component. This converts Java bytecode to machine code, which can then be loaded and executed in memory.

The Execution Engine is made up of three parts.

1- Interpreter-

The interpreter reads the bytecode instructions and sequentially executes them.
2- JIT Compiler-

The interpreter is used by the execution engine to execute the bytecode line by line, and the JIT compiler is used when it discovers repeated code. The JIT compiler then compiles all of the bytecode into native code.

3- Garbage Collector-

This checks the heap region for any unreferenced objects and destroys them to free up memory. As a result, it makes room for more items.

References

[1]K. Dinesh, Youtube.com, 2022. [Online]. Available: https://www.youtube.com/watch?v=GzFdNzLB7iI. [Accessed: 10- Jun- 2022].

--

--