Sunday, March 26, 2023

JVM Architecture In Java : An Excellent Guide for Beginners to Experts

One of the key features that make Java unique is the Java Virtual Machine (JVM). The JVM is an important component of the Java platform and plays a critical role in executing Java applications. In this article, we will take a closer look at the JVM Architecture in Java and explore its various components.

JVM Architecture In Java





JVM Architecture in Java


Overview of the JVM Architecture

The JVM is a software component that provides an environment for executing Java bytecode. It acts as a bridge between the Java code and the underlying operating system. The JVM is responsible for loading the bytecode, verifying its correctness, and executing it.

The JVM architecture is divided into three main components:
  1. Class loader Subsystem
  2. Runtime Data Area
  3. Execution Engine

Class Loader Subsystem

  • The Class Loader is Subset of JVM.
  • The Java Class Loader is part of the Java Runtime enviroment that dynamically loads Java Classes into JVM.
Class Loader Subsystem




The Class Loader is responsible for the following three tasks :
  1. Loading
  2. Linking
  3. Initialization

Loading 

  • It will read .class file and store corresponding information in the Method Area.
  • for each class file, JVM will store following information in the Method Area :-
  1. Fully Qualified Class Name.
  2. Fully Qualified Parent Class Name.
  3. Method Information.
  4. Variable Information.
  5. Constructor Information.
  6. Modifiers Information.
  7. Constant Pool Information.

There are three types of class loaders in the JVM:

Bootstrap Class Loader

This is the first class loader that is loaded when the JVM starts up. It is responsible for load the Java classes that are present in rt.jar file (rt.jar file present in bootstrap classpath i.e. jdk\jre\lib).

Extension Class Loader


This class loader is responsible for loading the Java classes from extension classpath (i.e. jdk\jre\lib\ext\*.jar).

Application Class Loader


This class loader is responsible for loading the Java classes from appliction classpath.it internally uses enviroment classpath.

Linking

In linking three activities are performed 
  1. Verification
  2. Preparation
  3. Resolution
Verification
  • In this process Byte Code Verifier checks whether the .class file is generated by valid Compiler or not and whether .class file is properly formatted or not.  
  • If verification fails, then JVM "java.lang.VerifyError" exception.
  • Because of this process, java is secured. 
Preparation

In this Process JVM will allocate memory for classlevel static variables & assign default values(not original value).

Resolution

In this process symbolic names present in our program are replaced with original memory references from runtime data area.

Initialization

In this process, two activities will be performed 
  1. All static variables are assigned with original values.
  2. static blocks will be executed from top to bottom.


Runtime Data Area

The runtime data area is the memory area where the JVM stores data while executing a Java application. 

Runtime Data Area




It is divided into several components:

Method Area

  • Method area is created when JVM started.
  • it stores .class file information and static variables.
  • per JVM one memory area, therefore multiple threads can access this area, so it is not thread safe.

Heap

  • Heap area is created when JVM is started.
  • It stores objects, instance variables and arrays (as every arrays is an object in java).
  • It can be accessed by multiple threads, so the data stored in heap area is not thread safe.

Java Stack

  • Whenever a new thread is created, a separate stack area will also be created. 
  • It stores the current running method and local variables.
  • When the method is completed, the corresponding entry from the stack will be removed.
  • After completing all method calls, the stack will become empty and that empty stack will be destroyed by the JVM before terminating the thread. 
  • The data stored in the stack is available only for the corresponding thread and not available to the remaining thread, so this area is thread safe.

PC Register 

  • PC Register Stand For Program Counter Register.
  • It holds the address of next executing instruction. 
  • For every thread, a separate pc register is created, so it is a thread safe.

Native Method Stack

  • All Native method calls invoked by the thread will be stored in the corresponding native method stack.
  • For every thread separate native method stack will be created.
  • It is also thread safe.

Execution Engine

The execution engine is responsible for executing the bytecode that is loaded by the class loader subsystem (java class file). 

Execution Engine






Types of execution engines in the JVM:
  1. Interpreter.
  2. Just-In-Time (JIT) Compiler.
  3. Garbage Collection

Interpreter

  • A module that alternately decodes and execute every statement or line in some body of code. 
  • The Java Interpreter decodes and executes bytecode for the JVM.

Just-In-Time (JIT) Compiler 

  • JIT stands for Just-In-Time which means that code gets compiled when it is needed, not before runtime.
  • The main purpose of JIT compiler is to improve performance.
  • JVM maintains a count as of how many time a function is executed. If this count exceeds a predefined limit or say threshold value, the JIT compiles the code into machine language which can directly be executed by the processor (Unlike the normal case in which javac compiles the code into bytecode and then java- the interpreter interrupts this bytecode line by line converts it into machine code and executes).
  • Also next time this function is calculated same compiled code is executed again unlike normal interpretation in which the code is interpreted again line by line. This makes execution faster.
  • JIT compilation is applicable only for repeatedly required methods, not for every method.   
JVM Architecture




Conclusion

The JVM is an essential component of the Java platform, and it provides a robust and secure environment for executing Java applications. Understanding the JVM architecture is crucial for Java developers since it allows them to optimize their code and improve application performance. By understanding how the class loader subsystem, runtime data area, and execution engine work together, developers can write efficient and reliable Java applications that run seamlessly on any platform.

You Know?

1. What is JVM architecture in Java?
  • The JVM (Java Virtual Machine) architecture is a critical component of the Java programming language. It is an abstract machine that provides a runtime environment in which Java bytecode can be executed.
  • The JVM architecture provides a platform-independent runtime environment that allows Java code to run on any platform that has a JVM installed, making Java a popular choice for cross-platform development.
2. What are the components in JVM architecture?
  • Class Loader.
  • Runtime Memory/Data Area.
  • Execution Engine.
3. Is JVM a compiler?

No, the JVM (Java Virtual Machine) is not a compiler. It is an abstract machine that provides a runtime environment for executing Java bytecode. The JVM does not directly compile Java source code into machine code, but it does perform some compilation at runtime.

4. What is the role of JVM?

The JVM provides a reliable and secure runtime environment for executing Java programs, with efficient memory management and performance optimization capabilities.

5. What are the 4 main tasks of JVM in Java?

the JVM performs several tasks to provide a reliable and secure runtime environment for executing Java programs. 
  • Loads the java code.
  • Verifies the code.
  • Executes the code.
  • Provides runtime environment.

No comments:

Post a Comment