Skip to content

Jvm architecture

🧠 JVM Architecture (Java Virtual Machine)

Think of the JVM as a tiny, disciplined city inside your computer. Java code walks in as raw text and walks out as executable instructions, guided through different β€œdistricts” that each have a job.


🧩 1. Class Loader Subsystem

This is the entry gate πŸšͺ

Image

Image

Image

Image

Image

Image

What it does:

  • Loads .class files into memory
  • Follows Delegation Model (parent-first approach)

Types of Class Loaders:

  • Bootstrap ClassLoader β†’ loads core Java classes (java.lang.*)
  • Extension ClassLoader
  • Application ClassLoader

Phases:

  1. Loading β†’ bytecode enters JVM
  2. Linking

    • Verification βœ”
    • Preparation βš™
    • Resolution πŸ”—
    • Initialization β†’ static variables assigned

πŸ—‚οΈ 2. Runtime Data Areas (Memory Structure)

This is the city’s storage system πŸ™οΈ

Image

Image

Image

Image

Image

πŸ”Ή Heap (Shared)

  • Stores objects
  • Managed by Garbage Collector
  • Example: new User()

πŸ”Ή Method Area (Shared)

  • Stores class metadata, static variables, constants

πŸ”Ή Stack (Thread-specific)

  • Each thread gets its own stack
  • Stores:

    • Method calls
    • Local variables

πŸ”Ή PC Register

  • Keeps track of current executing instruction

πŸ”Ή Native Method Stack

  • Used for native (C/C++) methods

βš™οΈ 3. Execution Engine

This is the brain + muscles combo πŸ’ͺ🧠

Image

Image

Image

Image

Image

Image

Image

Components:

🟒 Interpreter

  • Reads bytecode line by line
  • Slower but simple

πŸ”΅ JIT Compiler (Just-In-Time)

  • Converts bytecode β†’ native machine code
  • Improves performance πŸš€

🧹 Garbage Collector

  • Automatically removes unused objects
  • Keeps memory clean

πŸ”Œ 4. Native Interface (JNI)

Bridge between Java world and native world πŸŒ‰

  • Allows Java to call C/C++ code
  • Useful for system-level operations

πŸ“š 5. Native Method Libraries

  • External libraries (like .dll, .so)
  • Used by JNI

πŸ”„ JVM Flow (End-to-End)

.java β†’ (javac) β†’ .class (bytecode)
       ↓
Class Loader
       ↓
Runtime Data Areas
       ↓
Execution Engine
       ↓
Machine Code β†’ Output

🎯 Quick Memory Trick

  • Class Loader β†’ loads
  • Memory (Heap/Stack) β†’ stores
  • Execution Engine β†’ runs
  • JNI β†’ connects