How does Java Virtual Machine Works?

How does Java Virtual Machine Works?
Java was introduced as a new programming language because of its compatibility and nature to fit anywhere. That is why Java applications are known as WORA which means Write Once Run Anywhere. Because of JVM Java has this ability that a Java code written on one machine can run on any other machine that has JVM in it. The main task of JVM is to convert the Java code into Java byte code and compile it. The main task of JVM is to convert the Java code into Java byte code and compile it. This makes Java Development easy.

The first step of compilation is the .java file is to convert it into a .class file which is of the same name. This .class file then goes into various processes of compilation and verification before it actually runs. The compilation process takes four steps to complete.

The first block is of Class loader subsystem. It is responsible for three activities known as loading, Linking and initialization.
  • Loading

The task of the loader is to read the .class file, generate and save the Binary data in the method area. It stores mainly three types of information into the method area. The first one is the name of a fully qualified loaded class and its immediate parent class. The next is that it checks if the .class file is related to class, interface or enum. At last, it checks the information related to modifier, variables, and method.

  • Linking

The task of a linker is to do verification, preparation and sometimes resolution. The first step of Linking is to verify the .class file and check the file format. The second phase is the memory allocation for class variables and memory initialization to default values. The final step is to replace symbolic reference to direct reference.

  • Initialization

This is the last stage or block of Class loader system. In this stage, all the static, variables are assigned to some definite value in the code. There are mainly three types of class loaders. Bootstrap class loader which loads trusted classes. Extension class loader is the next step after bootstrap class loader which capable of loading classes present in extension directories. System class loader is the next step after extension class loader. It is capable of loading classes from application class paths.

The next block of JVM is the JVM memory. JVM memory can be divided into five different sections where each section has different tasks.

The five different blocks are Method area, Heap Area, Stack Area, PC registers and Native method stacks.
  • Method Area

This area is used to store all the data related to class level such as class name, immediate parent class name, information regarding message and variables.

  • Heap Area

Heap Area is another shared resource like Method area which stores all the information related to objects.

  • Stack Area

JVM creates a run time stack for every thread which is stored here. After termination of each thread one run time stack is deleted.

  • PC Registers

It stores the address of the instruction that is being executed right now.

  • Native method stacks

A separate native stack is created for every thread.

References

Leave a comment