Nearby lessons
5 of 125Java - JVM Architecture
- What the JVM does step by step
- The Class Loader Subsystem
- The memory areas: stack, heap, method area, PC registers
- The Execution Engine (Interpreter + JIT)
- Stack vs Heap — the classic interview question
JVM Architecture is a core concept of the Java language. This lesson explains What is the JVM?, Class Loader Subsystem and Runtime Data Areas (The Memory Map) with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.
What is the JVM?
The JVM (Java Virtual Machine) is a program that executes Java bytecode (.class files). It is the heart of Write Once, Run Anywhere — because the same bytecode runs on any machine that has a JVM.
The JVM has three main jobs:
- Load the .class file (Class Loader Subsystem).
- Store the data in memory (Runtime Data Areas).
- Execute the bytecode (Execution Engine).
Class Loader Subsystem
The class loader finds and loads .class files into memory. It does three things for each class:
- Loading — reads the .class file and creates the Class object.
- Linking — checks the bytecode (verification), prepares memory, and resolves references to other classes.
- Initialization — runs static variables and static blocks.
Important: class loading is lazy — a class is loaded only when the program first uses it. This is the dynamic loading we discussed in Chapter 1.
Runtime Data Areas (The Memory Map)
The JVM divides memory into five main areas:
| Memory area | What it stores | Shared or per-thread? |
|---|---|---|
| Method Area | Class definitions, methods, static variables, constants | Shared by all threads |
| Heap | All objects and arrays (Garbage Collector's territory) | Shared by all threads |
| Stack | Local variables and method calls of ONE thread (each call = one frame) | Per thread (each thread has its own stack) |
| PC Registers | Address of the instruction currently being executed | Per thread |
| Native Method Stack | Information for native (C/C++) method calls | Per thread |
Stack vs Heap — The Interview Favourite
| Point | Stack | Heap |
|---|---|---|
| Stores | Local variables, method call frames | Objects and arrays |
| Memory type | Primitive values + references to objects | The actual object data |
| Lifetime | Lives while the method runs | Lives until GC collects it |
| Size | Small, fixed per thread | Big, shared |
| Access | Fast | Slower (indirect via reference) |
| Who manages | Automatically on method entry/exit | Garbage Collector |
Execution Engine — Interpreter and JIT
The execution engine actually runs the bytecode. It uses two machines together:
- Interpreter — reads bytecode one instruction at a time and runs it. Simple but slow.
- JIT (Just-In-Time) Compiler — watches which methods run again and again, and compiles those hot methods into fast machine code. That is why Java becomes faster the longer it runs.
In short: the interpreter starts quickly, and the JIT speeds up the frequently used code. Together they give Java both quick startup and high performance.
Garbage Collector — The Third Member
The Execution Engine also includes the Garbage Collector, which automatically frees the heap memory of unused objects (Chapter 22 covered it in detail). The JVM as a whole, then, is: Class Loader + Memory Areas + Execution Engine (Interpreter, JIT, GC).
One More Concept — Platform Independence in Action
What Really Happens When You Run `java FirstApp`
The java command activates the JVM, which then works like this:
- JVM takes the main class name (FirstApp) from the command prompt.
- JVM searches for the class in three places: current folder, Java's predefined library, and folders in the classpath environment variable.
- If not found → error Could not find or load main class FirstApp.
- If found → JVM loads the bytecode into memory using Class Loaders.
- JVM searches for the main() method. If missing → Error: Main method not found in class FirstApp.
- If main() is found → JVM creates a Main thread and starts executing the program.
And the classpath? If your .class files live in another folder, tell JVM where:
- JVM = Load (Class Loader) + Store (Runtime Areas) + Execute (Execution Engine).
- Five memory areas: Method Area, Heap, Stack, PC Registers, Native Stack.
- Stack holds local variables and references; Heap holds actual objects.
- Objects in the heap are freed by the Garbage Collector.
- Execution Engine = Interpreter (starts fast) + JIT (speeds up hot code).
- The JVM being OS-specific is what makes bytecode platform independent.