JVM – Java Virtual Machine Working and Architecture

In this Java tutorial, we are going to discuss Java Virtual Machine(JVM), which is one of the most important aspects of Java programming language.

We will study about JVM architecture and its subsystems. We also briefly discuss what a Java Virtual Machine is and how it works.

Moreover, we will discuss Java Classloader, an executive engine in Java, the Java Memory Model, and the JVM screen in detail.

What is JVM – Java Virtual Machine?

As we all know that we need to either compile or interpret any source code before executing it. But Java uses the combination of both, i.e, compiler as well as interpreter.

Java programs are first compiled into Java Byte Code(Binary form) and then a special Java interpreter interprets them for a specific platform.

Java ByteCode is the machine language for Java Virtual machine(JVM). The JVM converts the compiled binary byte code into a specific machine language.

Java Virtual machine acts as a subpart of Java Runtime Environment(JRE).

The JVM is an abstract machine that works on the top of existing processes. We can implement it in hardware or software.

JVM combined with Java APIs makes Java Platform. The essential use of the JVM is HotSpot.

We can define JVM in three ways:

1. A specification that specifies the working of Java Virtual Machine. The one who provides the implementation is independent to choose the algorithm.

2. An Implementation called the Java Runtime Environment (JRE).

3. A Runtime Instance that creates a JVM instance when you write a command on the command prompt to run the java class.

Java Virtual Machine

Working of JVM

JVM(Java Virtual Machine) behaves as a run-time engine to run Java applications. JVM calls the main method present in Java code.

Java Virtual machine(JVM) is a part of the JRE(Java Runtime Environment).

Java applications are WORA (Write Once Run Anywhere). This means that we need to write the Java programs just once, which we can run on different platforms without making changes in the Java program.

When we compile a .java file, the compiler generates the .class files(contains byte-code) with the same names as that of the class present in a .java file.

When we run a .class file, it goes through various steps.

Working of JVM

Need for Java Virtual Machine

The need for Java Virtual Machine is to handle the tasks that it performs which are necessary for the development of programs involving Java. Java virtual machine performs the following tasks:

  • Loads the code.
  • Verifies code.
  • Executes the code.
  • Provides a run-time environment for various applications.
  • JVM provides a Memory area.
  • Provides a Register set.
  • JVM provides the garbage collection heap.
  • Reports fatal errors.
  • Provides a class file format

All these tasks make the Java virtual machine an integral part of Java development.

JVM Architecture in Java

There are three main subsystems in JVM Architecture:

1. ClassLoader

2. Memory Area

3. Execution Engine

JVM Architecture

Now, we will understand each of them in detail.

Java ClassLoader

JVM Model

It is the component of JVM Architecture that loads classes in memory. Every JVM consists of a ClassLoader. Three built-in classloaders in Java are:

1. Bootstrap ClassLoader

This is the classloader which is the super class of Extension classloader. It loads the rt.jar file.

2. Extension ClassLoader

It is the ClassLoader which loads the jar files present in the directory. This is the child Bootstrap classLoader and parent of System classloader.

3. System/Application ClassLoader

It is the classLoader which loads the class files from the classpath. This is the child of the Extension classloader.

The Three important functions of ClassLoader are Initialization, Loading, Linking.

1. Initialization

This operation involves the assignment of all static variables with their specific values in the block of the program.

In this phase, there is an assignment of all static variables with their values defined in the code and static block.

The execution takes place from top to bottom in a class and from parent to child in the class hierarchy.

2. Load4ing

This operation loads files from secondary memory into the main memory (RAM) for execution. The Classloader reads the .class file, generates the corresponding binary data, and saves it in the method area.

JVM stores some information for each .class file in the method area. This information is:

  • The fully qualified name of the loaded class and its immediate parent class.
  • Whether .class file is related to interface or an enum or a class.
  • Modifier, Variables and Method information, etc.

After loading the .class file, JVM creates an object of type Class to represent this file in the heap memory.

The programmers can use this Class object for getting class level information like the name of the class, parent name, methods, and variable information, etc.

To get this object reference we can use the getClass() method of Object class.

3. Linking

This operation combines different files in the main program together. It performs verification, preparation, and (optionally) resolution.

  • Verification: The Verification phase checks the correctness of the .class file. It means that it checks that the file formation and generation is by a valid compiler or not. If the verification fails then we get a java.lang.Verify Exception.
  • Preparation: JVM allocates memory for class variables and initializes the memory to default values.
  • Resolution: Resolution is the process of replacing symbolic references with direct references. It uses searching into the method area to locate the referenced entity.

Code to demonstrate ClassLoader in Java:

Let us see an example to print the classloader name

package com.techvidvan.jvm;
public class Test1 {
  public static void main(String[] args) {
    // Let's print the classloader name of the current class.   
    //Application/System classloader will load this class  
    Class c = Test1.class;
    System.out.println(c.getClassLoader());
    //If we print the classloader name of String, it will print null because it is an  
    //in-built class which is present in rt.jar, Bootstrap classloader loads it. 
    System.out.println(String.class.getClassLoader());

Output:
sun.misc.Launcher$AppClassLoader@659e0bfd
null

JVM Memory area

1. Method Area– It stores the structure of each class like method data, field data, runtime pool, metadata.

2. Heap– Heap is the runtime area where object allocation takes place.

3. Stacks– Stacks store the partial results and local variables of a program. Whenever a thread is created, there is a simultaneous creation of JVM stack. When we invoke a method, a new frame creates and destroys at the same time when the invocation process completes.

4. PC Registers – It stores the address of the currently executing JVM instruction.

5. Native Method Stacks – It includes all the native methods required in any application. It is not written in java.

Execution Engine in Java

It is the component of JVM that reads data from memory locations and executes the instructions. It has three major components namely a virtual processor, an interpreter, and a JIT compiler.

1. Virtual Processor

2. Interpreter: Read the bytecode stream then execute the instructions.

3. Just-In-Time(JIT) compiler: It improves performance. JIT compiles parts of the byte code with similar functionality at the same time and reduces the amount of time needed for compilation.

Native Method interface

It is a framework that helps in communication between different applications written in different languages such as c, c++, etc.

Native Method Interface allows Java code running in a JVM to call by libraries and native applications.

Method Libraries

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

Difference Between JDK, JRE, And JVM

Difference Between JDK, JRE, & JVM

The following are a few important differences between JDK, JVM, and JRE.

1. JVM stands for Java Virtual machine, JDK stands for Java development kit, and JRE stands for Java runtime environment.

2. JDK is for the development environment whereas JRE is for the run time environment.

3. JVM runs inside the JRE environment. JRE contains class libraries, Java Virtual Machine and other files other than development tools like compiler and debugger.

4. JRE is a subset of JDK. JDK contains JRE along with development tools such as compiler, debugger, etc.

Important Points

  • JVM stands for Java Virtual Machine that drives the Java Code. It converts Java bytecode into machine language.
  • Here, Java code is compiled to bytecode. This bytecode gets interpreted on different machines
  • JIT stands for a Just-in-time compiler. JIT is part of the Java Virtual Machine. It increases the execution time
  • Java may be slow in execution compared to other compiler machines,

Conclusion

Here we come to the end of the JVM tutorial, where we learned about the Java Virtual machine in detail.

Moreover, we discussed the architecture and its subtypes. We also learned the differences between the important terms- JDK,JRE, and JVM.