Components of the Java Virtual Machine

Taking a look into the the internals of the JVM

Aaron De Zoysa
5 min readMar 3, 2022
JVM architecture. Image by Author via Canva.com

The Java Virtual Machine (JVM) is the component that allows us to run a java program. So how exactly does this work to ensure that are programs are safe to run? In this article we will be taking a look into each of the components inside the JVM.

We will be taking a look at the following components:
1. Class Loader
2.
Memory/Runtime Data Area
3.
Execution Engine
4.
Native Method Interface (JNI)
5.
Native Method Library

Class Loader

Internals of class loader. Image by Author via Canva.com

The class loader in the JVM is mainly known as being responsible for loading the compiled classes into memory, but it also has many other tasks that it carries out. The responsibilities of the class loader can be divided into three categories namely; loading, linking and initialization.

Loading

The loading section of the class loader can be further divided into different class loaders, but we will not be talking about that in this article. This section is what is responsible to load the class into the memory area and to do that it will have to undergo a few other tasks. The class loader will check for the following in a class when loading it into memory:
1. Fully qualified name.
2. If it is a class, interface or an enum.
3. Immediate parent.
4. Instance variable information.

Once the class has been loaded, the JVM will create an object of class data type and will assign the loaded class into this object. This only happens once at the first time the class has been loaded and is then stored in the heap.

Linking

The linking phase is further divided into three stages that it will follow before moving into the next phase of the class loader. These stages are; verification, preparation and resolution. We will take a look into these stages in further detail now.

  • In the verification stage, the JVM will check if the class is safe to execute or not. It does this through the bytecode verifier that has a specific set of rules to check the class against. It will check the validity of the compiler, the structure as well as the formatting of the class. If any of these checks fail then the JVM will throw a VerifyException indicating that the class is not safe. Otherwise, it will move on to the next stage.
  • The preparation stage is where the JVM will allocate memory as well as assign default values to the static variables and fields of classes and interfaces.
  • The resolution stage is a very helpful stage for developers as it is where all symbolic references between objects are replaces with direct references. This happens because the JVM will not understand the human readable language that we use when we create classes and objects with references.

Initialization

This is the final phase of the class loader and it is where all the static variables (that were set to default values in the preparation stage) will be assigned their actual values. This phase will also call the constructor of the class a well as execute any static blocks.

Memory / Runtime Data Area

Components of JVM Runtime Data Area. Image by Author via Canva.com

The memory area component in the JVM is further divided into five more components. From these five components, the method area and heap area are created at the start of the JVM and there will be only one of each per JVM. The stack area, PC register and native method stack will be created for every thread in the JVM. Let us take a deeper look at each of these individual components.

Method Area

When a class is loaded, all the class information is stored in this method area including the code for methods and constructors of the class.

Heap Area

All object data will be stored in this heap area when they are loaded. It allocates the required memory for the class instances as well as for arrays.

Stack Area

The stack area will store data like the method calls and local variables per thread. For every new method call a frame is created in the stack and it will remain here until the program leaves the scope of the method, then it will destroy that specific frame.

Program Counter (PC) Registers

The PC register holds information of the JVM instruction being executed for a specific thread. This gets updated with the next execution when the current instruction has been executed.

Native Method Area

This is a stack that can support native methods that are written in a different language.

Execution Engine

Components of Execution Engine. Image by Author via Canva.com

Now that the classes have been loaded and the required memory is allocated, the next step is to run the program which is the task carried out by the execution engine. This is divided into three components that we will be looking into further.

Interpreter

The interpreter reads bytecode line-by-line and executes it at that time. It does not store it for future use.

JIT Compiler

The JIT compiler is used in conjunction with the interpreter so that repeated code can be compiled into native machine code that can be used directly by the JVM without any performance issues.

Garbage Collector

The garbage collector is used for collecting and removing objects from the heap area when they do not have any reference to them. This occurs automatically to make efficient use of memory by freeing up space for new objects. The garbage collector can be called manually as well when needed.

Native Method Interface (JNI)

The Java Native Interface (JNI) is useful as it supports the use of non-Java programming language packages when it is needed to write code that is not supported by Java.

Native Method Libraries

These native libraries can be loaded through the JNI a they are written in different programming languages than Java

I hope this article provided you with useful information on the inner components of the JVM. Thanks for reading!

References

Videos

JVM Part 02 — Inside Java Virtual Machine -Krish Dinesh

JVM part 04 — ClassLoader -Krish Dinesh

Articles

JVM Tutorial — Java Virtual Machine Architecture Explained for Beginners

--

--