Java Virtual Machine

Hansini Rupasinghe
7 min readMay 3, 2021

--

Virtual — Something that does not exist in reality.

Machine — A device or an item that helps you to do your work.

Java Virtual Machine — It does not exist physically or as entity wise. JVM can neither be installed or uninstalled as it is just a program. Although you can install JDK and JRE, it is impossible to install JVM.

There are 2 types of Virtual Machines.

Fig 1
Fig 2
Fig 3

✯ JVM is an Application-based Virtual Machine.

✯ JVM is a specification. (JRE is the implementation)

✯ When you download JRE, JVM comes with it. It deploys all the codes to create a JVM.

✯ If you install JRE on a Windows/Mac machine, it will create the code required to create a JVM for Windows/Mac environment.

✯ JVM takes care of converting bytecode into machine code.

✯ At the moment you execute a Java program, it creates a JVM instance on your machine. JVM instance dies when program exits.

execute 3 different programs at the same time ➜ 3 different JVM instances

If you have a program called “helloworld”;

Fig 4 : To Compile the Code

Then it creates a java class file. (helloworld.class file)

Fig 5 : To run

✯ When a JVM instance is created, a non-daemon thread will be created.

✯ Your class must have a method called “public static void main”. When your JVM instance creates, it takes this method to execute.

Fig 6
Fig 7

Components inside JVM

  1. Class Loader
  2. Memory Area
  3. Execution Engine
Fig 8

Class Loader

✯ Main Responsibility — Taking the class and loading into the memory area.

2 types of Class Loaders in JVM

  1. Bootstrap Class Loader
  2. Custom-defined Class Loader

Responsibilities

  1. Loading
  2. Linking
  3. Initialization

Loading

✯ Main Responsibility — Taking your class file and put into the memory.

Before loading into the memory area, JVM reads class file’s;

➤ Fully qualified class name

➤ Variable Information

➤ Immediate parent information

➤ Whether this is a class, interface or an enum

✯ When loading each and every class, JVM creates an object from the class type and stores on heap only for the very first time. (Data Type ➝ Class)

✯ Only 1 object per class

Linking

Fig 9

Initializing

✯ It assigns real value.

✯ Execute your static block.

✯ JVM enforces the implementation that initialization must be done before each class active use.

Active use of a class in Java

  1. ‘new’ keyword
  2. when a static method is invoked
  3. when assigning a value for a static field
  4. Initial class (main)
  5. Reflection API (getInstance)
  6. Instantiate sub class

✯ JVM has a rule : Before any of the above mentioned active use, it must go through the initialization phase.

4 ways Java can initialize its class

  1. ‘new’ keyword
  2. clone();
  3. getInstance(); using reflection API
  4. IO.ObjectInputStream();

Memory Area

✯ When you load the class, it will load all the class information into the method area.

✯ Keeps class / type information.

Heap Area

✯ When you load the class from method area, all the data of the objects come to heap area.

✯ Holds object information.

Stack

✯ Keeps method information and local variables.

✯ 1 stack per thread.

✯ When you go into a method, it creates 1 frame in the stack.

✯ When it exits the method, the frame will also be popped.

PC Registers

✯Hold information about the next execution if it is not a native method.

Native Method Area

✯Provides facility to hold the native method and method information if you are accessing any native method from your program.

Execution Engine

Execution Engine can be considered as a major component of Java Virtual Machine (JVM). It aids the communication between different memory areas of JVM. And also it is used to execute Java Class files. One of the main purposes that Execution engine does is executing byte code that is assigned to the run time data areas in JVM through class loader.

There are 3 main components in Execution Engine which are used to execute Java classes.

  1. Interpreter

The main purpose of the interpreter is to read the bytecode and interpret into the machine code (native code) and to execute sequentially. This reduces the performance of the system as it interprets every time no matter if it is the same method. JIT compilers came into the picture to solve this issue.

2. JIT Compiler

JIT Compiler was introduced with the aim of improving the performance while counterbalancing the slow execution of interpreter. It compiles bytecode into native machine code at runtime. Although the start up time is slow, the end result is very good in performance optimization.

3. Garbage Collector

This is a program used in Java to manage the memory automatically. A daemon thread always runs in the background. The garbage collector is used to destroy the unreachable methods in heap memory while freeing it up.

Data Types in VM

✤ Data resides in Memory Area.

Fig 10

Difference between language primitive types and JVM data types

Boolean

● In JVM data types, Boolean is always represented either by int or byte.

operatable boolean ➞ int

array of boolean ➞ byte

● Boolean ‘false’ is represented as 0 and Boolean ‘true’ is represented as non zero.

Long

● Always takes 64 bit 2’s complement as its size.

● It doesn’t matter what the underlying platform is, it does not change based on the platform.

● It takes the same size in every implementation.

Return Address Type

● Developers don not get access to this.

● Specific to JVM.

● Cannot find this in the language.

● Similar to the implementation of ‘Final’ keyword.

Fig 11

Additional Notes:

✦ A program that is written to run on a server or a different machine may not exactly run the same way on a mobile device without modifications.

✦ Even though Java is platform independent, JRE is tightly platform dependent.

JRE — Java Runtime Environment

✽ A set of software tools used for developing Java applications.

✽ Implementation of JVM.

✽ Physically exist.

✽ Platform dependent.

JDK — Java Development Kit

✽ Platform dependent.

✽ Contains tools for developing, debugging etc.

Word Size

✽ A base / unit

✽ Need to assign a value to word size in order to explain.

✽ Length of word size is decided by the particular JVM implementation.

2 rules to define length of word size

➣ The particular word should be able to hold any primitive data type.

➣ Two words should be able to carry long or double value.

✽ Word should be at least length of 32 bits. Otherwise cannot get into these rules.

Static Variable

✽ Belongs to the class and not to object (instance)

✽ Initialized only once at the start of the execution.

Instance Variable

✽ Declared inside the class, but outside a method.

✽ When space is allocated for an object in heap, a slot for each instance variable value is created.

Conclusion

● There are many types pf VMs.

● JVM falls under Application-based VMs.

● There’s nothing that really exists as a JVM.

● JRE contains a JVM. When you install JRE, it deploy codes to create JVM.

● When you run your program, JVM reads your class file and converts to a language that can be understood by your O/S.

● Daemon thread is capable of creating other non-daemon threads.

● JVM dies if there are no non-daemon threads.

● Size of memory area depends on each VM implementation.

● Arrays reside in heap.

● Data types in JVM are almost identical with data types of Java language except for return address type.

● Java has a bytecode verifier in JVM that checks if a class is safe to execute or not.

● When you compile, JVM creates a new method called ‘init’ for each constructor.

● What is inside init() method is depending on how your class structure is.

● 3 kinds of code in init()

  1. Code to invoke init() of some other constructor
  2. Code to initialize instance variables
  3. Bytecode to a particular implementation.

References

--

--