What is JVM (Java Virtual Machine)?

Nafees
8 min readJul 18, 2022

--

Before we learn what is the JVM, first we have to Understand About What is java, what is Virtual Machine

Java is a very popular and high-level programming language. It is object oriented, class oriented and platform independent. Using Java, it can be written codes very easily and clearly. It’s a server-side language that uses to most back-end development.

Once we code a java program, while it runs it is converted to a bytecode of the program into a specific machine language. Since, the language the Java program is written, cannot be identified by the machine.

A Virtual Machine (VM) is a compute resource that uses software instead of a physical computer to run programs and deploy apps. One or more virtual “guest” machines run on a physical “host” machine. Each virtual machine runs it’s own operating system and functions separately from the other VMs, even when they are all running on the same host.

There are 2 categories of Virtual Machine.

1. SVM (System based virtual machine)

2. AVM (Application virtual machine).

SVM — System-based virtual machine. The system contains one hardware. But it creates multiple environments that are completely independent from each other. This is based on the hardware.

Eg:- Hypervisor, Xen

AVM — Application based virtual machine. This does not contain any hardware. But using this environment, it can be used to get some inputs from language and produce the output some kind of different language that can be understood. This is an application or a program. This cannot be touched since it’s like a program. So, this is called as Process-based virtual machines also.

Eg:-

JVM (Java Virtual Machine)

CLR (Common Language Runtime) — that helps .NET to create virtual environment.

PVM (Parrot Virtual Machine) — it creates virtual environment for dynamic languages, lightweight and very efficient

JVM (Java Virtual Machine)

JVM is a virtual machine is falling into application based virtual machine. we can’t download JVM because that is not exist. JVM is a program. JVM is a complete specification. When we install JRE that has JVM also in it. When java program is start that time JVM instance created and JVM convert byte code into machine code that OS can understand the code and when program ends JVM also destroyed.

Java is a platform independent language but JRE is platform independent because when we install on JRE in windows machine that JVM is deploying codes to windows environment.

we install the JRE on Mac machine that JVM deploying codes belong to Mac environment

Who develops and maintains the JVM?

The JVM is widely deployed, heavily used, and maintained by some very bright programmers, both corporate and open source. The Open JDK project is the offspring of the Sun Microsystems decision to open-source Java. Open JDK has continued through Oracle’s stewardship of Java, with much of the heavy lifting these days done by Oracle engineers.

Memory management in the JVM.

The most common interaction with a running JVM is to check the memory usage in the heap and stack. The most common adjustment is tuning the JVM’s memory settings.

Architectural Diagram of JVM

Inside the JVM has parts. They are method area, heap, stack, native method, PC registers.

when the java program starts to run then one JVM instance will created and first JVM loading all the classes into the method area.

Objects are loading into the heap area. Those method area and heap areas are each and only one for JVM. We see the stack, comes local variables, block variables.

Its thread base process. when we going into the one method that create one frame in stack. we go to other method another frame will create in stack.

If one method is finish its execution, then that frame will destroy in stack area. PC registers have information about next execution of methods except native methods. It’s also creating per thread.

There two ways to destroy JVM instance.

1. If the non-daemon thread exits.

2. Application call the system exit method.

1. Class Loader

The class loader is a subsystem used for loading class files. It performs three major functions viz. Loading, Linking, and Initialization.

2. Class Area or Method Area

JVM Method Area stores class structures like metadata, the constant runtime pool, and the code for methods.

3. Heap

All the Objects, their related instance variables, and arrays are stored in the heap. This memory is common and shared across multiple threads.

4. JVM language Stacks

Java language Stacks store local variables, and it’s partial results. Each thread has its own JVM stack, created simultaneously as the thread is created. A new frame is created whenever a method is invoked, and it is deleted when method invocation process is complete.

5. PC Registers

PC register store the address of the Java virtual machine instruction which is currently executing. In Java, each thread has its separate PC register.

6. Native Method Stacks

Native method stacks hold the instruction of native code depends on the native library. It is written in another language instead of Java.

7. Execution Engine

It is a type of software used to test hardware, software, or complete systems. The test execution engine never carries any information about the tested product.

8. Native Method interface

The Native Method Interface is a programming framework. It allows Java code which is running in a JVM to call by libraries and native applications.

9. Native Method Libraries

Native Libraries is a collection of the Native Libraries (C, C++) which are needed by the Execution Engine.

Data Type in JVM

There are 2 data types

1. Primitive data type — always holding the data.

2. Reference data type — always hold in the reference.

Primitive data type

All the language primitive types are same as JVM primitive data types but that small difference in Boolean because even though it’s come to the primitive type but that is fulfill all instructions. In JVM Boolean represent Int or byte. Boolean false represents 0 and true is represent non 0.

If it is an opera table Boolean, then it represents by Int value and that array of Boolean then t represents array of byte value. Size of datatypes are same in every envy but long is always take 64 bit 2s compliment as its size doesn’t matter what environment. Other primitive data type is return address that is final block.

References data Type

We see reference data type they are class, Interface, Array, word. Class type is always reference to the class instance. Interface always reference to class implementation.

null reference data type it is specific type that does not reference anywhere. Word is a size is just unique.

Length of word size define by two rules.

1.it should be able to hold primitive data type.

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

According to these rules word should be length of 32 bit. Overall data type is nothing but it almost identical with java language data type except return address types that come new thing when we discuss about JVM architecture.

JVM-Class Loader

Class loaders are responsible for loading Java classes dynamically to the JVM (Java Virtual Machine) during runtime.

They’re also part of the JRE (Java Runtime Environment). Therefore, the JVM doesn’t need to know about the underlying files or file systems in order to run Java programs thanks to class loaders.

There are 2 types of class Loader

1. Bootstrap Class Loader — A Bootstrap Class Loader is a Machine code which kick starts the operation when the JVM calls it.

2. Custom-Define Class Loader A Custom Class Loader can maintain a separate Class Loader for each developer’s servlet.

There are 3 types of class Loader Responsibilities.

1. Loading

2. Linking

3. Initialization

Loading

Main Responsibility of that Take class file and put into the Memory.

When JVM is reading the file, it will do the following tasks.

o Read Fully Qualified Class name

o Read about variable information

o Read about immediate parent information

o Check for class or interface or enum.

When class loaded in to memory area JVM creates an object from class type. There will be only one object per class. Very first time only JVM create object from the class type and its storing in the heap.

Linking

There are 3 step in linking

1. Verification

2. Preparation

3. Resolution

· Verification

Java has a bytecode verifier in JVM from there it will exactly check whether this class is safe to execute.

The “byte code verifier” will check the code for

o Tampering

o Validity of the compiler

o Code structure and format

If any of these are not satisfied JVM throw runtime exception called verify exception.

· Preparation

Default values will be assigned to all the class variable. There is a default value for each data type.

o Object- null

o Boolean- false

o Int- zero(o)

· Resolution

It is the process of replacing symbolic references with direct references from type. It is done by searching within the method area to find the referenced entity.

Initialization

This is the last stage of class loading. In this phase, all static and instance variable are assigned actual values. There is a rule that every class must be initialized before any active usage.

There are 6 active uses as follows.

1. using new keyword. (Example: Vehicle van=new Vehicle();).

2. invoking a static method.

3. assigning value to a static field.

4. if a class is an initial class (class with main()method).

5. using a reflection API (getInstance()method).

6. initializing a subclass from the current class.

There are four ways of initializing a class and they are,

1. using new keyword — this will go through the initialization process.

2. using clone(); method — this will get the information from the parent object (source object).

3. using reflection API (getInstance();) — this will go through the initialization process.

4. using IO.ObjectInputStream(); — this will assign initial value from InputStream to all non-transient variable

How Java Class Loader and JVM arguments work

Java is incapable to load two different classes with the same class name. For example, If you give two classes but from the same class name identical class name JVM load only one class. So because always class nowadays take the unique class name.

I have referred the following YouTube playlist to write this article,which was made by Krishantha Dinesh

Thank You

Catch Up Next One…..

--

--

Nafees

Undergraduate Student of University of Jaffna || Associate Software Engineer