Java Virtual Machine

JVM is an engine that provides a run time environment to convert byte code to machine language. It stands for Java Virtual Machine.

So the Java code is first compiled into bytecode. Then this bytecode gets interpreted on different machines. JVM is available for various h/w and s/w platforms and hence can be considered platform dependent.

JVM is also responsible for allocating memory space.

Java was developed with a concept of WORA (write once run anywhere) , java code is compiled into a .class file and then that class file is loaded by the JVM and it further executes it.

JVM Architecture

JVM is divided into 3 main subsystems:

  1. Class Loader Subsystem
  2. Runtime Data Area
  3. Execution Engine

How JVM works ?

1. Class Loader

It is a Subsystem , It is mainly responsible for three activities.

  • Loading
  • Linking
  • Initialization

Loading : The Class loader reads the .class file, generate the corresponding binary data and save it in method area( where it stores the fully qualified name of the loaded class and its immediate parent, it also stores the variables, modifiers and method information).

Linking : In this process, JVM performs verification(to ensure correctness of .class file), performs preparation(allocates memory to class variables) and performs resolution(where the symbolic references are replaced by direct references).

Initialization : In this process, all the static variables are assigned values defined in the code or in the static blocks from top to bottom from parent to child.

There are three built-in classloaders in Java.

  1. Bootstrap ClassLoader: This is the first classloader which is the super class of Extension classloader. It loads the core java APIs present in JAVA_HOME/jre/lib directory. It loads the rt.jar file which contains all class files of Java Standard Edition like java.lang package classes, java.net package classes, java.util package classes, java.io package classes, java.sql package classes etc.
  2. Extension ClassLoader: This is the child classloader of Bootstrap and parent classloader of System classloader. It loades the jar files/classes located in the extension directories $JAVA_HOME/jre/lib/ext directory.
  3. System/Application ClassLoader: This is the child classloader of Extension classloader. It loads the class files from classpath. By default, classpath is set to current directory. You can change the classpath using “-cp” or “-classpath” switch. It is also known as Application classloader.

2. Run Time Data area/ JVM Memory Area

Method Area : There is only one Method Area in a JVM and it is a shared resource. It holds class level information like (class name, immediate parent class name, methods and variables info etc).

Heap Area : There is only one Heap Area per JVM and it is a shared resource as well. Class Objects are stored here.

Stack Area : It is not a shared resource but For every thread or process JVM creates one run time stack which is stored here. This stack area stores local/temporary variables. When a thread terminates its run time stack is also destroyed by JVM.

PC Registers : It stores the instruction information of executed and to be executed instructions, it is separate for each thread.

Native Method Stack : This area holds the instructions of native code

3. Execution Engine

Execution engine execute the .class (bytecode). It reads the byte-code line by line, use data and information present in various memory area and execute instructions. It can be classified in three parts

Interpreter : It interprets the bytecode line by line and then executes. The disadvantage here is that when one method is called multiple times, every time interpretation is required.

Just-In-Time Compiler(JIT) : It is used to increase efficiency of interpreter.It compiles the entire bytecode and changes it to native code so whenever interpreter see repeated method calls,JIT provide direct native code for that part so re-interpretation is not required,thus efficiency is improved.

Garbage Collector : It destroy un-referenced objects.For more on Garbage Collector,refer Garbage Collector.

4. Native Method Interface

It enables java code to call or be called by native applications. Native applications are programs that are specific to the hardware and OS of a system.

5. Native Method Libraries

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

Difference between JDK, JRE & JVM

JREJVMJDK
JRE is the environment within which the java virtual machine runs. JRE contains Java virtual Machine(JVM), class libraries, and other files excluding development tools such as compiler and debugger. Which means you can run the code in JRE but you can’t develop and compile the code in JREJVM runs the program by using class, libraries and files provided by JREJDK is a superset of JRE, it contains everything that JRE has along with development tools such as compiler, debugger etc.

Summary

In this article we learnt about the Java Virtual Machine and its Architecture, we also learnt about how JVM works and the difference between jdk,jvm & jre.
Hope you liked the article !


Leave a Comment