Nearby lessons

5 of 125

Java - JVM Architecture

📌 What You Will Learn
  • 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).
In simple words: The JVM is a program that runs your compiled bytecode. The same .class file works on any machine, because every operating system has its own JVM that understands that bytecode.
Example01
JCode Cell
1 .class file
2 |
3 Class Loader Subsystem (loads the class)
4 |
5 Runtime Data Areas (stack, heap, method area...)
6 |
7 Execution Engine (Interpreter + JIT + GC)
8 |
9 YOUR PROGRAM RUNS

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 areaWhat it storesShared or per-thread?
Method AreaClass definitions, methods, static variables, constantsShared by all threads
HeapAll objects and arrays (Garbage Collector's territory)Shared by all threads
StackLocal variables and method calls of ONE thread (each call = one frame)Per thread (each thread has its own stack)
PC RegistersAddress of the instruction currently being executedPer thread
Native Method StackInformation for native (C/C++) method callsPer thread
In simple words: The JVM splits memory into five areas, each with a fixed job. The heap and method area are shared by all threads; the stack, PC register and native method stack belong to one thread each.

Stack vs Heap — The Interview Favourite

PointStackHeap
StoresLocal variables, method call framesObjects and arrays
Memory typePrimitive values + references to objectsThe actual object data
LifetimeLives while the method runsLives until GC collects it
SizeSmall, fixed per threadBig, shared
AccessFastSlower (indirect via reference)
Who managesAutomatically on method entry/exitGarbage Collector
In simple words: Primitives and references go to the stack, actual objects go to the heap. int y = 20 sits on the stack, but new Demo() lives in the heap and only its address is stored on the stack.
Trainer's Note: Golden rule for the interview: primitives and references live in the stack, actual objects live in the heap. One reference in the stack can point to one object in the heap. When the reference dies (method ends), the object becomes garbage in the heap.
Example04
JCode Cell
1class Demo {
2 int x = 10; // x lives in HEAP (inside the object)
3 
4 void method() {
5 int y = 20; // y lives in STACK (local variable)
6 Demo d = new Demo(); // d (reference) in STACK, Demo object in HEAP
7 }
8}

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.

In simple words: The interpreter starts the program fast, and the JIT makes the hot methods fast. The JIT watches which code runs again and again and compiles it into machine code — that is why Java speeds up the longer it runs.

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

Example07
JCode Cell
1You write: Hello.java
2Compile: javac -> Hello.class (bytecode)
3 
4On Windows: Windows JVM runs it
5On Linux: Linux JVM runs it
6On Mac: Mac JVM runs it
7 
8One bytecode, many machines - because each OS has its own JVM.

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:

Example08
JCode Cell
1set classpath=E:\myclasses; // or on one command:
2java -cp E:\myclasses FirstApp
📝 Key Takeaways
  • 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.

🧠 Test Your Knowledge

4 Questions
Progress: 0 / 4