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 πͺ
What it does:
- Loads
.classfiles 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:
- Loading β bytecode enters JVM
-
Linking
- Verification β
- Preparation β
- Resolution π
- Initialization β static variables assigned
ποΈ 2. Runtime Data Areas (Memory Structure)¶
This is the cityβs storage system ποΈ
πΉ 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 πͺπ§
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