JVM Architecture


The JVM (Java Virtual Machine) architecture is a crucial part of the Java Runtime Environment (JRE) that enables Java applications to run on any device or operating system without modification. Here’s a breakdown of its core components:

Absolutely! Let’s walk through a simple Java program and see how it flows through the JVM architecture step by step.

Java Program Example

public class HelloWorld {
  public static void main(String[] args) {
     System.out.println("Hello, JVM!");
  }
}

🔄 Flow Through JVM Architecture

1. Compilation

  • The Java file HelloWorld.java is compiled by the Java Compiler (javac) into bytecode.
  • Output: HelloWorld.class (contains platform-independent bytecode).

2. Class Loader Subsystem

  • The Class Loader loads HelloWorld.class into the Method Area.
  • It verifies the bytecode, allocates memory for static variables, and prepares the class for execution.

3. Runtime Data Areas

  • Method Area: Stores class structure (methods, variables).
  • Heap: Allocates memory for objects (though this program doesn’t create any).
  • Java Stack: A new stack frame is created for the main method.
  • PC Register: Points to the current instruction being executed.
  • Native Method Stack: Not used in this simple example.

4. Execution Engine

  • The Interpreter starts executing bytecode line by line.
  • The JIT Compiler may optimize frequently used code (not likely in this tiny program).
  • The line System.out.println("Hello, JVM!"); is executed:
    • System.out is a reference to the standard output stream.
    • println is a method that prints the string to the console.

5. Output

  • The string “Hello, JVM!” is printed to the console.

6. Garbage Collection

  • After execution, any memory used (if any objects were created) is cleaned up by the Garbage Collector.

### 🧠 Summary of Flow

        Source Code (.java)
              ↓
        Compiler (javac)
              ↓
        Bytecode (.class)
              ↓
        Class Loader → Method Area
              ↓
        Execution Engine → Stack, Heap, PC Register
              ↓
        System.out.println → Console Output
              ↓
        Garbage Collector (if needed)

Great! Let’s walk through a slightly more complex Java program that touches on almost all major components of the JVM, including:

🧩 Sample Java Program

public class JVMExample {
static int staticCounter = 0; // Stored in Method Area

public class JVMExample {
 static int staticCounter = 0; // Stored in Method Area
 
 public static void main(String[] args) {
  JVMExample obj = new JVMExample(); // Object stored in Heap
  obj.instanceMethod(5);             // Stack frame created
  System.out.println("Done");        // Native method call
}

 public void instanceMethod(int value) {
  int localVar = value * 2;       // Stored in Stack
  staticCounter++;                // Access static variable
  helperMethod(localVar);         // Another stack frame
}

 public void helperMethod(int data) {
  String message = "Value is: " + data; // String in Heap
  System.out.println(message);          // Native method call
}
}