The 5 Main components of JVM

Charin Perera
5 min readMar 3, 2022

This article is about Class Loader, Memory Area, Execution Engine , Java Native Interface and Native Method Libraries the 5 main components of JVM.

The inside of the JVM

1. Class Loader

If you think about it what Class Loader means is to simply load the class, into the memory, but in reality it is much more complex than that.
The class loader has three main categories:

1. Loading

Main objective is to take class file and put it into the memory, to do this there are several steps that needs to happen.
* Reads the fully qualified class name.
* Reads whether it is an enum, class or interface
* Reads what it’s immediate parent is
* Reads instance variable information

When loading a class the first the above mentioned information will be read, after reading all information of the class, the JVM will create an object from the ‘CLASS’ data type and the object information of the instance of the class will be put into the ‘HEAP’ in the Memory area. If there is another object created from the same class as before, then there will not be another CLASS type object created but the one created by the JVM before will be used.

2. Linking

The linking part of the Class Loader will be divided again into three other categories:

  1. Verification
    When a java class is being loaded, it will be put through a program called the ‘byte-code verifier’, it will verify whether the class comes through a valid compiler, whether it had correct structure and correct formatting. If any of the checks are not passed a Runtime Exception called ‘VerifierException’ will be thrown.
  2. Preparation
    Once the verification has been done correctly, it will move on the preparation stage, where if there any instance level variable (static variables) in the class, a default value(NOT the initial value)for the variables will be assigned in the preparation stage.
  3. Resolution
    What happens here is the replacement of anywhere the created object is used with the specific memory location of the object, as Java allows for domain specific class (Different class for different situations), but since JVM does not know what the domain is for, the memory location of the object will be used.

3. Initialization

It here where variables will be assigned their intended initial values (Not the default value as in the preparation stage of linking), if there are any assigned to them in the program. It is also here that the static blocks in the class will be executed.

When loading a class to the JVM, it allow for the implementation of LOADING, LINKING and INITIALIZATION to occur either sequentially or parallelly, but as a limitation, the implementation is forced by the JVM that INITIALIZATION must happen before each class can be actively used.
A class is considered to be in active use when :

  • using the ‘new’ keyword
  • invoking a static method
  • assigning a value to a static field (If the field is FINAL it is not an active use)
  • if it is the initial class (main)
  • using the reflection framework
  • instantiating sub class

Anything else is considered to be in passive use, which does not require to pass the INITIALIZATION process.

There are multiple ways of creating an object for a class and for each way, the way that the initial values are assigned to them are different.

  1. new keyword
    Will go through the initialization process.
  2. Clone() method
    The values will be taken from the SOURCE object (Parent)
  3. getInstance() method in the reflection framework
    Will go through the initialization process.
  4. IO.ObjectInputStream
    Assign values from input stream to all non-transient variables

2. Memory Area

Simply it is the place where JVM stores the classes, which is loaded from the Class Loader, and is kept until executed.

However, even if the task of the memory area is simply to store classes, there is much more happening inside of it. Where the Memory area is branched off into more sub-areas, which are:

  1. Method Area
    When a class is loaded, all class information (i.e.: class type) will be loaded here and will be stored.
    There is only one Method Area per JVM.
  2. Heap Area
    When a class is loaded, all objects/instances will be loaded here and will be stored.
    There is only one Heap Area per JVM.
  3. Stack
    All method information (i.e. : local variables) is loaded and stored here. For each method, in the stack, there will be separated frames created in the stack, which is why data from one method cannot be accessed from another directly.
    Stacks are created per thread, which means for each thread running there will be one Stack created.
  4. PC Registers
    If the method being executed is not a NATIVE method (When executing your own methods), the PC Registers will hold information of next execution.
    This too, like stacks will be created per thread.
  5. NATIVE Method Area
    If you are accessing any NATIVE method from program, the NATIVE Method Area will be the one providing the facilities to store the NATIVE method and the NATIVE method information.
    This too, like Stacks and PC Registers will be created per thread.

3. Execution Engine

After the byte-code has been loaded into the memory area by the class loader and whatnot, we now have something to execute, the execution engine will take care of that, what it does is it simply reads through the byte-code a line at a time in units, however the byte-code will be in human readable format, therefore the machine will not be able to read it, and will be needed to be converted into machine-readable format, to do that the following three components will be used:

  1. The Interpreter
    This will execute the bytecode sequentially, while a command-line query makes call with a compiled file as it’s argument, it is a fast process, compared to the JIT Compiler
  2. JIT Compiler
    This is one of the most important components in the JRE which increases the run time performance of the application, this is important because no other component has an impact on performance as much as the JIT compiler, this will be activated anytime a Java method is called and is a default compiler.
  3. Garbage Collector
    What is does is pretty straight forward, where it searches for any garbage and gets rid of it. What garbage is in this context will be the unused objects in the JVM Heap area. The process will, on the first scan it will check every possible object available in the Heap area, and if it is not used it will be marked to be removed, and afterwards it will go on sweeping all the marked objects.
    This process is completely automatic and there is no need for the programmer to be involved in this in anyway.

4. Java Native Interface (JNI)

What this does is that it enable Java to use Native methods (C, C++) while writing Java programs. This is achieved by the JNI by interacting with the Native Method Libraries, which is implemented in C, C++ and will provide those libraries to the execution engine.
This allows to directly access assembly code. JVM will have two different types of codes, which is Java and Native, and the JNI will create a ‘well-defined’ link between the two.

5. Native Method Libraries

This is a collection of native libraries which will be required by the Execution Engine.

Ref:

Java Virtual Machine | Various Components of Java Virtual Machine (educba.com)
JVM Part 02 — Inside Java Virtual Machine — YouTube
JVM part 04 — ClassLoader — YouTube

--

--