JAVA Virtual Machine

Oshan Nanayakkara
7 min readJun 9, 2022

What is a virtual Machine?

Virtual means it is not in the reality, which means it simulate an environment to be real to the person.

Machine means it a device which is help to do our task without doing the work on ourselves.

In java we have heard there is something called JAVA Virtual Machine. But this cannot be installed or delete by the user. Because it is virtual. It is not a tangible or it not exist in the real world.

When talking about the Virtual Machines there are 2 main types of Virtual Machines that exist.

SVM — System Based Virtual Machine

AVM — Application Based Virtual Machine

SVM — System Based Virtual Machine

SVM has a hardware which is tangible that create an environment which allows work for the multiple users or multiple instances over that. Since there are more than 1 environments, these environments are independent.

ex:- Hypervisor, Xen

AVM — Application Based Virtual Machine

In these kind of Virtual Machines there is no tangible hardware like SVMs. It has application instead of that. This application help to create a platform that allow to run other programs. This platform get some inputs and then it convert it into different output or a language which someone can understand. Because of this process it is known as Process Based Virtual Machine.

ex:- JVM, CLR, PVM

JVM is fall under this Application Based Virtual Machine. It is a specification which tells how things done.

We cannot directly download this JVM from the internet because it actually not exist in the real world, but it comes with the JAVA Runtime Environment(JRE) which we can download from the internet easily. When installing the JRE in our PC, it deploys the necessary code to establish the JVM in the pc.

JAVA is platform independent but JRE is platform dependent.

When we run a JAVA program, JVM will read the class files and then convert it to a language which operating system can understand.

Life of JVM

When we run a program it create a JVM instance in the computer and that JVM instance dies when we exist from the program.

When we run 2 or more programs at the same time then each and every one of those program has separate JVM to it.

After the creation of JVM instance, it create an non-demon thread. Each java program has public static main method. This previously created JVM instance use this main method to execute the java program and it continues. Now we have a demon thread which is capable of creating other non demon threads.

This JVM instance can be destroyed by using 2 methods.

If there is no non-demon thread is exist within the program which means we destroyed every non-demon thread we created. Then this JVM instance will automatically dies.

By calling system exit method.

Inside JDK

(JDK in Java — GeeksforGeeks, 2022)

As you can see from the above image JVM is located inside the JRE. Inside this JVM there are 3 main components.

Class Loader - load

Memory Area - store

Execution Engine - execute

Inside Memory Area

(rawat, 2022)

This memory are is consist of 5 main parts.

Method Area - Per 1 JVM. This loads the class information when loading

Heap Area - Per 1 JVM. After loading all the objects data comes here.

Stack - Per 1 thread. This keep the method information and local variables.

PC register - Per 1 thread. If executing not native method then this will hold the information about the next execution.

Native Method area - Per 1 thread. This provide facility to hold native methods.

JVM data types

There are 2 main data types in the JVM which are primitive types and reference types.

Primitive types — This hold the value of data itself.

Reference types — This hold the reference.

This primitive types can be further divide into 3 parts. Such as Numeric, Boolean and Return address.

Numeric - Has 2 types such as Floating (float, double) and Integral types(byte, short, int, long, short).

Boolean - Boolean false is zero and true is non zero.

Return Address - Implementation of final keyword use this return address type.

Reference types can be divide into 3 parts.

Class types - This reference to instance of class.

Interface - Reference to the instance of the implementation class of the particular interface.

Array types - Reference to arrays.

Null use to not reference.

WORD SIZE

It is a base which is need to assign a value to the word size. To explain that length of the WORD SIZE is decided by the particular implementation. This word size should be able to hold any primitive. To hold primitive this WORD size should be at least 32 bits. 2 WORD should be able to hold double or long values.

Class Loader

Class loader has the responsibility of taking the class and load it to the memory area.

There are 3 main tasks by the class loader, those are Loading, Linking and initialization.

There are mainly 2 types of class loaders such as Bootstrap Class Loader and Custom Design Class Loader.

Loading — What happens here is that it take the class file and put that into memory. This reads fully qualified name whether it is a enum or class or interface, immediate parent and instance variable information. It will go read those information and load them into the memory area.

Linking- There are 3 main process done in this. Those are Verification, Preparation and Resolution.

Verification- This check whether the class file is altered or not. If altered then it will throw verification exception. In here it check if valid compiler creates this, is the classfile is in the correct format, is the classfile in the correct structure.

Preparation- Assign default value to the static variable.

Resolution- Program use domain specific words in the program (ex:- Student, Employee). JVM converts this words to understandable for machine. Instead of those keywords JVM replace those with the memory locations which can be understand to the machine.

Initialization- This assign real value.

6 occasions when active use happens.

If process with new keyword.

Invoke static method.

Assign value for static field.

If it is a initial class.(class with main method).

If using reflection API to create object.(getinstance() method)

If instantiating subclass.

If a class subject to any of the above 6 it must go through initialization process. Before any active use it must create a object.

When we compile the above class. Compiler create new method which is not accessible for developers. That method has no name in it. Compiler put instance level variables to it(name, id). If class has any static variables those also put into that class which is created by the compiler. If there is no instance variables or static variables then that class is not created.

There are 4 ways which java initialize a class

Use new keyword — This will go through initialization process and it will assign initial value to those variables.

Use clone method from existing object — This will get the value from source object to assign the initial value.

Use reflection API (getInstance() method) — Go through initialization process and it will assign initial value to those variables.

IO.ObjectInputStream() with this class and can get a object — Assign value from input stream for non-transient variable

How java go though constructor and instantiating parent class

parent of this class is human

When compile this code JVM create init() method for each constructor. Inside init() method decided(depend) by what is inside the constructor. If constructor(child class) has nothing inside it then that init method has right to invoke parent class(human) default constructor’s init method.

There can be 3 kinds of code inside this init method

code to invoke some other constructors init method

code to initialize instance variables.

has bytecodes to particular implementation

JVM Arguments/ JAVA Classpath

Java unable to load 2 different classes with the same name. In that occasion JVM only load 1 class. because classloader take unique class names.

If we have 2 classes in 2 directories with same class name but their content inside is different.

And then we create the jar file and when executing it using the classpath.

First we set the class path of Application.java file in the “a” directory and then set the class path of Application.java file in the “b” directory.

output — prints the content in a/Application.java

If we switch the class path by setting Application.java file in the “b” directory and then set the class path of Application.java file in the “a” directory.

output — prints the content in a/Application.java

This happen because JVM consider what comes first. The first class path execution occasion add the second class path execution at the end. Then the output of first occasion shows in the terminal. This happens if we do this in the same terminal. But this can be solved by below 2 steps.

By setting class path empty and reverse the order

use new terminal (setting Application.java file in the “b” directory and then set the class path of Application.java file in the “a” directory.)

output — prints the content in b/Application.java

Conclusion — If we given 2 identical classes in the classpath, JVM consider what comes first. The first class in the classpath is what JVM going to execute. This changes when we given JVM Arguments, In that occasion JVM take the last class in the class path.

References

GeeksforGeeks. 2022. JDK in Java — GeeksforGeeks. [online] Available at: <https://www.geeksforgeeks.org/jdk-in-java/> [Accessed 9 June 2022].

rawat, R., 2022. Memory areas in JVM. [online] B2eprogrammers.com. Available at: <https://b2eprogrammers.com/java/memory-areas-present-inside-JVM> [Accessed 9 June 2022].

--

--