19. What is Java Virtual Machine (JVM)

Prabath Shalitha
5 min readJul 15, 2022

--

In this article, let’s talk about what is Java Virtual Machine (JVM) and how it works.

Virtual machine (VM)

Before getting into JVM, let’s see what is a Virtual machine (VM). Simply VM means a machine that does not exist physically. There are 2 types of VMs.

  1. System VM — One or multiple hardware that creates multiple completely independent environments. Eg. Hypervisor, Xen
  2. Application VM (Process VM) —creates a platform to run other programs. No hardware involves. It converts inputs into different outputs. Eg. JVM, CLR, PVM

Java Virtual Machine (JVM)

We can simply say that JVM is the specification because it says how things should be done. As mentioned earlier, since JVM is virtual we can’t install it. Instead, we need to install JRE (Java Runtime Environment). When we install JRE it will deploy all codes needed to create a JVM.

JRE is platform-dependent. This means we need to install operating system-specific JRE for the different operating systems. When we run the java bytecode(.class file), JRE will deploy the code to create JVM instance. Every java program will create one JVM instance. If we are not executing a Java program inside the computer there is no JVM inside it at that time. When the moment we end the Java program, JVM will also die. If we run 3 different Java programs at the same time, it will create 3 different JVMs inside our computer.

Let’s dig deep around to see how things work. When we have the java source file with the .java extension, we can use the javac fileName.java command to compile it. Then it creates a file containing bytecode with .class extension. Then we can run it using java fileName command. When this command is given it commands the computer OS to create a new JVM instance. When it creates there is a non-deamon thread created inside it. Then JVM looks for the public static void main method and starts the execution statements inside the file. When that non-deamon thread dies JVM also dies.

JVM has 3 main sections to do their jobs to convert bytecode into machine code (run the program).

  1. Class Loader (Loading)
  2. Memory area (Storing)
  3. Execution engine (Executing)

01. Class Loader

There are 3 steps to load the class.

  1. Loading
  2. Linking
  3. Initialization

Loading

When we load the class into JVM, it checks the following things :

  • Fully qualified class name
  • Instance variable information
  • Immediate parent information
  • Whether it is a class or interface or enum.

After this, it creates a Class type object (type is Class) and stores it in heap.

Linking

There are 3 steps under the linking process.

  1. Verification — The bytecode verifier will check whether the class is safe to execute. Therefore, it should pass the some conditions like correct structure, correct format,and valid compiler. Otherwise, it will throw runtime exception called verifier exception.
  2. Preparation — Assign default values to static and instance variables. (the default value for an integer is 0, and Boolean is false)
  3. Resolution — In the java program we create objects with their real names. But machine will not understand this. So JVM will replace the symbolic link with its direct link i.e replace the object with its memory address.

Initialization

Assign real values(actual value) to static and instance variables. And the static block is also executed. The initialization process should be executed before the following 6 active uses.

  1. Use of a new keyword
  2. Invoking static method
  3. Assigning value to the static variable
  4. Invoking reflection API — getInstance()
  5. Invoking main method (Initial class)
  6. Instantiating sub-class.

There are 4 ways to initialize a class.

  1. using new keyword
  2. clone()
  3. getInstance()
  4. IO.ObjectInputStream

02. Memory area

There are 5 parts to JVM internal memory.

  1. Method area — Class information, type information, static and instance variable information will be stored in this area. JVM will create only one method area for program execution.
  2. Heap area — Object references will be stored here. All object data including string and array are also stored. Also, only one memory area is created for the program.
  3. Stack — Method information, local variable information will be stored. A stack frame will be created for each method. When a method is finished, that frame will pop out from the stack. So the particular method’s local variables will no longer exist.
  4. PC register — Hold next method execution information. PC register is created per thread. When a method uses the native method, that thread will have a null value in the pc register.
  5. Native method area — Native method information will be stored. When java does not provide the functionality that you want to use, there you have to use the native method, which is implemented in other programming languages. (C/C++).

03. Execution Engine

This is the final job of JVM. It has 3 sub-components.

  1. Interpreter — reads the bytecode and interprets it into machine code line by line and executes in a sequential manner. If one method is used multiple times, it interprets that method multiple times, this reduces the system performance and makes slow execution. To overcome this problem JIT compiler is used parallelly with the interpreter.
  2. JIT compiler (Just in Time compiler) — improves the performance of the java programs by compiling bytecode into machine code at runtime. Directly calls the compiled code in which places the repeated method is used, this reduces the amount of time needed for compilation.
  3. Garbage collector — a daemon thread that runs in the background, and manages the memory automatically. Frees up the heap memory by destroying unreachable methods.

--

--

Prabath Shalitha

Graduated from SLIIT in Software Engineering. Working as an Associate Software Engineer at Virtusa.