JVM

JVM(Java Virtual Machine): JVM is a part of JRE(Java Runtime Enviroment). Jvm is responsible for call the main method present in java code.

Image Source: Wikipedia.

When we compile .java file it will generate .class(Byte code) with same name. When we run it it will go to several steps, these steps together describes JVM.

Class Loader: Class loader loads the class information in the method area. Class loader responsibilities are below.

  1. Loading
  2. Linking
  3. Initialization

Loading: Class loader reads the .class file and generates the binary data then save it in the method area. It loads the following information in the method area for every .class file.

-> Fully qualified class name and its immediate super class name.

-> Whether .class file related to Class or Interface or Enum.

-> Modifiers, variables and method information etc.

After loading .class file JVM creates the object for the Class which is from java.lang package. This Class object represents the class loaded .class file. Using this object we can get the class level information of the loaded class. We can get Class object from the getClass method of Object class.

Linking: It performs verification, preparation and resolution.

-> It verifies the correctness of the .class file. i.e it checks the file format is proper or not and also is it compiled with proper compiler or not. If verification fails then we will get java.lang.VerifyError runtime exception.

-> It allocates the memory for the instance variables and assign them with their default values.

Initialization: In this phase all the static variables assigned with its values and static blocks if any from the top to button and from the parent class to child class.

Basic class loader are below.

  1. Bootstrap class loader: It is capable of loading trusted classes. It loads core java api classes which are present in the jre/lib directory. This directory popularly know as bootstrap path. Every JVM must implement bootstrap class loader. It is implemented in native languages like C/C++.
  2. Extension class loader: It is a child class loader of bootstrap class. It is responsible for loading the classes which is present in the jre/lib/ext directory or java.ext.dirs property set path. It is implemented by the sun.misc.Launcher$ExtClassLoader class.
  3. Application/System class loader: It is a child class loader of extension class loader which is responsible for load the class present in the application path. It is internally uses the class path(java.class.path). It is implemented by the sun.misc.Launcher$ExtClassLoader class.

JVM follow Delegation-Hierarchy principle to load classes.

https://i0.wp.com/d1hyf4ir1gqw6c.cloudfront.net//wp-content/uploads/JVM1.png

Image Source: http://javarevisited.blogspot.in/2012/12/how-classloader-works-in-java.html

JVM Memory:

Method Area: In method area classes information will be stored like class name, immediate super class name, variables(including static variables) and method informations. Only one method area per JVM and it is shared memory.

Heap Memory: Information of all the objects will be present here. Only one heap area per JVM and it is shared memory.

Stack Memory: For every thread JVM creates a stack memory, here local variables of the method will be stored and when thread is terminated the corresponding stack memory will be cleared by the JVM. It is not a shared memory.

PC Registers: Each thread as its own pc registers and it holds the address of current execution of a thread.

Native Method Stack: It stores the native methods information. Every thread has its own native method stack.

Execution Engine: It reads the .class file(byte code) line by line and use the data or information present in different memory areas then executes the instructions.

Execution engine consists of 3 parts.

  1. Interpreter: It interprets the byte code line by line then executes. It needs more time when we call method multiple time for interpretation.
  2. Just In Time(JIT) compiler: It compiles the byte code and converts it into native code, so whenever interpreter requires multiple class then JIT provides native code to the interpreter so re-interpretation is not required. Because of it efficiency of interpreter will be improved.
  3. Garbage Collector: It is used for the clear the memory of object which are no longer used and their reference is referred to null.

Java Native Interface(JNI): It is a interface which interacts with the Native method libraries and it provides this libraries for execution.

Native Method Libraries: These are native libraries developed in C/C++ which are required for the execution engine.

Leave a comment