Corporate Training Study Material: Java Virtual Machine (JVM) Architecture

Rashmi Mishra
0

 

Corporate Training Study Material: Java Virtual Machine (JVM) Architecture

📌 Session Title: Java Virtual Machine (JVM) Architecture

  • Duration: 1 Hour
  • Audience: Corporate Employees, Software Developers, Java Beginners
  • Learning Objectives:
    Understand the role of JVM, JRE, and JDK
    Learn how Java code is compiled and executed
    Deep dive into JVM architecture and its components

📖 Module 1: Introduction to Java Virtual Machine (JVM)

1.1 What is JVM?

📌 JVM (Java Virtual Machine) is an abstract machine that enables Java programs to run on any device or operating system. It is responsible for:
Converting Java bytecode into machine code
Managing memory allocation & garbage collection
Ensuring Java follows its "Write Once, Run Anywhere" principle

📌 JVM is platform-dependent, but Java code is platform-independent.


📖 Module 2: Understanding JDK, JRE, and JVM

2.1 JDK (Java Development Kit)

A development environment to write, compile, and run Java applications.
Includes JRE + Compiler + Debugging Tools
Used by developers for Java application development.

JDK Components:

  • javac → Java Compiler
  • java → Java Application Launcher
  • javadoc → Java Documentation Tool
  • jar → Java Archive Utility

📌 Popular JDKs: Oracle JDK, OpenJDK, Amazon Corretto, IBM JDK


2.2 JRE (Java Runtime Environment)

A runtime environment to execute Java programs.
Includes JVM + Core Java Libraries.
Used by end-users to run Java applications (e.g., Web browsers with Java Applets).

JRE Components:

  • JVM (Java Virtual Machine) → Converts bytecode to machine code.
  • Core Libraries → Java APIs like java.lang, java.io, etc.
  • Runtime Libraries → Required dependencies to run Java programs.

📌 If you only need to run Java applications, install JRE.


2.3 JVM (Java Virtual Machine)

The heart of Java responsible for executing Java bytecode.
Converts .class files (bytecode) into machine code.
Manages memory allocation & garbage collection.

JVM Components:

  • Class Loader → Loads .class files
  • Memory Area → Heap, Stack, Method Area, PC Register
  • Execution Engine → Converts bytecode into native machine code
  • Garbage Collector → Frees unused memory

📌 JVM is platform-dependent, but Java bytecode runs on any JVM.


📖 Module 3: JVM Architecture - How Java Code is Compiled and Executed

3.1 Java Compilation and Execution Process

🔹 Step 1: Write a Java Program (Hello.java)

public class Hello {

    public static void main(String[] args) {

        System.out.println("Hello, World!");

    }

}

🔹 Step 2: Compile Java Code

javac Hello.java

The javac compiler converts Hello.java to Hello.class (bytecode).

🔹 Step 3: Run Java Bytecode

java Hello

The JVM reads Hello.class and executes it on the machine.

📌 Key Point: Java runs in two steps – Compilation (to bytecode) and Execution (by JVM).


3.2 JVM Internal Architecture (Detailed Breakdown)

🛠 1. Class Loader Subsystem
Loads Java classes into memory (.class files).
Performs Bytecode Verification for security.
Uses three class loaders:

  • Bootstrap Class Loader (Loads core Java classes, e.g., java.lang.*).
  • Extension Class Loader (Loads Java extension libraries, e.g., javax.crypto).
  • Application Class Loader (Loads user-defined classes).

🛠 2. Runtime Memory Areas (JVM Memory Model)

📌 JVM divides memory into different sections:

Memory Area

Purpose

Method Area

Stores class metadata, method bytecode, static variables.

Heap Memory

Stores objects and class instances.

Stack Memory

Stores method call stack, local variables.

PC Register

Stores memory address of the executing instruction.

Native Method Stack

Handles OS-specific native code execution.


🛠 3. Execution Engine
Converts Java bytecode to native machine code.
Uses Just-In-Time (JIT) Compiler for performance optimization.

📌 JIT Compiler (Just-In-Time)

  • Improves speed by compiling frequently used bytecode into machine code.
  • Uses techniques like Method Inlining, Loop Unrolling, Dead Code Elimination.

🛠 4. Garbage Collection (Memory Management)
JVM automatically reclaims unused memory using Garbage Collection (GC).
Types of GC Algorithms:

  • Serial GC (Single-threaded, for small apps).
  • Parallel GC (Multi-threaded, for performance).
  • G1 GC (Advanced, for large-scale applications).
  • ZGC & Shenandoah GC (Low-latency garbage collectors).

📌 Best Practice: Always release unused objects to avoid memory leaks.


📖 Module 4: JVM Performance Tuning & Optimization

4.1 JVM Tuning Parameters

Use JVM options to monitor & optimize performance:

java -Xms512m -Xmx1024m -XX:+UseG1GC -XX:+PrintGCDetails

-Xms → Initial heap size
-Xmx → Maximum heap size
-XX:+UseG1GC → Enable G1 Garbage Collector
-XX:+PrintGCDetails → Show garbage collection logs


📖 Module 5: Hands-on Lab & Practical Session

Demo 1: Installing and Configuring JDK
Install JDK, set JAVA_HOME, and configure PATH.

Demo 2: Writing & Running a Java Program
Compile & execute Java code using javac and java commands.

Demo 3: Debugging JVM Memory Usage
Use jconsole and VisualVM to monitor JVM performance.


📖 Summary: Key Takeaways

JVM is the runtime engine that executes Java bytecode.
JRE provides runtime libraries, while JDK includes tools for development.
JVM consists of Class Loader, Runtime Memory, Execution Engine, and Garbage Collector.
Java code is compiled to bytecode and executed by JVM using Just-In-Time (JIT) compilation.
Garbage Collection (GC) manages memory efficiently and prevents memory leaks.

📌 "Write Once, Run Anywhere" is possible because Java runs on JVM!

 

Tags

Post a Comment

0Comments

Post a Comment (0)