Java Virtual Machine

Java Virtual Machine (JVM) is works as runtime environment for java applications. One responsibility of the JVM is convert byte code into machine language. JVM is one part of Java Runtime Environment (JRE). JVM act as specification and JRE act as its implementation. If there is one program running then there is only one JVM and if there is 4 java programs running there will be 4 JVM which means the count of JVM running in your machine is equal to number of programs you are currently running in your machine. Java is known for write once run anywhere concept which means developer can write a java program in one system and can expect to run it any other systems because when we compile java program we get .class file and we can expect that class file run in any JVM in any system it can be windows, Linux or Mac. Because of this java is called as platform independent. Even though java in platform independent Java Development Kit (JDK) which includes JVM and JRE is platform dependent. There are separate JDK for separate Platforms. Since we are talking about JVM lets see the architecture of the JVM.

Visual structure for JVM

Class Loader

Class loader is sub system in JVM which used to load class files. Java classes would not load at once what class loader does is it load class file when it needed. And there are three main responsibilities for class loader which are loading, linking and initialization. Let’s see brief discussion about these three

Loading

The main thing loading phase does is read the “.class” file put those data into method area. When loading a class for the first time there are certain thing it does.

  • Load its fully qualified package name for example “com.test.sample”.
  • Then it read about instance variable information.
  • Also it read about immediate parent information.
  • And also it read about whether it is a class, interface or Enum.

After loading the “.class” file to method area JVM create object which class type is “Class”. JVM creates this object to represent current class in heap memory.

Linking

This step performs another sub steps they are verification, preparation and resolution. And they are mention below.

  • Verification.

This verify correctness of the class file by using byte code verifier. This will verify that this code is in valid format and valid structure from byte code and if it doesn’t satisfy it will throw Runtime Exception call “java.lang.VerifyError”.

  • Preparation.

JVM allocate memory for instant variables and initialize the default values for it.

  • Resolution.

This will replace a memory location to the class which you created, if you created class call “Student” and it will replace to all the places you used “Student” with that memory location.

Initialization

In initialization phase all static variables are assigned by their real values and execute static blocks. This happens top to bottom approach and parent to child hierarchy.

And there is another important part in JVM which called as JVM memory area. And memory area consist five main parts which are described below.

Method Area

In this phase class loader will load all the information of the class to the method area. Some of the information are about class name of current processing class, immediate parents of the class which means if that class extend from anther class it keep information about it if nothing by default every class parent is Object class, information about methods and variables and about static variables. And there is only one memory area per JVM this shares resources within the application.

  • Heap Area

Heap area is about to keep information about the objects in the class. There is only one Heap area per JVM which shares all the information within the application.

  • Stack Area.

JVM create stack for each thread it is not common to the application. JVM creates run-time stack which will store here. Every block of this stack call stack frame which used to store every method call this also like typical stack Last in First out (LIFO). All the method local variables stores in corresponding stack frame. After the termination of thread JVM will destroy that thread belonging stack. And these stacks are not shared.

  • Pc Register.

Pc Register is holding the address of current execution thread. Each thread has its own thread and pc registers are not shared.

  • Native method stack.

Every thread has separate native method stack and this use to store about native method details.

These five parts consist in the memory area and there is another main part in the JVM and it called as execute engine.

Execute Engine

Execute engine executes the bytecode (.class). This reads byte code one line by line. Execution engine uses various different data and information available in different memory area and executes the given information and this can be divide to 3 parts.

  • Interpreter.

This reads byte code line by line and then executes it. And there is disadvantage in this interpretation is when one method is called multiple times then each and every time this interpretation is also required.

  • Just In Time Compiler (JIT)

Just in time compiler use to increase the efficiency of an interpreter. Just in time compiler compiles the entire byte code and converts it to native code. After that whenever interpreter shows a repeated method call, just in time compiler gives the direct native code for the repeated method call so re interpretation is not required. Doing such thing improves the efficiency of the program.

  • Garbage Collector

Garbage collector destroys unreferenced objects. In java there is no need to think about the objects which are no longer in use and garbage collector responsibility is to destroy those objects in order to free the heap memory. There are four ways that garbage collector to consider as unreachable object and they are mentioned below.

  • Nullifying the reference variable which means assigning null to the variable which holds an object before.
  • Re assigning the variable which means if we had a variable which holds an object then we assign another object to the same variable and the previous object which hold by that variable will unreachable.
  • Objects creates inside method which means method local variable which scope is only valid until the end of that method.
  • Island isolation which mean an object which holds many of the objects as instance variables inside that object. When the main object is nullified or reassigned which will isolated and all the objects inside that object will also unreachable this is called island isolation.

These are the main parts and main execution process of the JVM and the brief overview of the architecture of the JVM.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store