Java Interview Prep Series — #4 JVM Internals: How Java Works Behind the Scenes
If you’re learning Java or preparing for Java interviews, understanding the Java Virtual Machine (JVM) is one of the most important steps toward understanding how Java actually works internally.
We write Java code, compile it, and run it.
But what happens inside the JVM after we execute our program?
Let’s understand the major components of the JVM and how they work together.
1. What is JVM?
JVM stands for Java Virtual Machine.
The JVM is the runtime engine responsible for executing Java bytecode.
When we compile Java source code:
HelloWorld.java
↓
javac
↓
HelloWorld.class
↓
BytecodeThe JVM takes this bytecode and executes it on the underlying machine.
The JVM is responsible for several important tasks:
- Loading classes
- Managing memory
- Executing bytecode
- Garbage Collection
- JIT compilation
- Thread management
- Interaction with native code
A simplified view of the JVM is:
JVM
|
┌─────────┴─────────┐
↓ ↓
Class Loader Runtime Data Areas
|
┌───────┴────────┐
↓ ↓
Heap / Stack Other Areas
|
↓
Execution Engine
|
┌───────┴───────┐
↓ ↓
Interpreter JIT
|
↓
Native Machine Code2. Major Components of JVM
The major components we need to understand are:
- Class Loader Subsystem
- Runtime Data Areas
- Execution Engine
- Native Method Interface
- Native Method Libraries
Let’s understand each one.
3. Class Loader
The Class Loader is responsible for loading .class files into the JVM.
When you run a Java application, the JVM needs to load the classes required by the program.
The Class Loader:
- Finds
.classfiles - Loads class information into JVM memory
- Performs linking-related steps
- Initializes classes when required
Classes are generally loaded lazily, meaning they are loaded when they are needed rather than loading every possible class at application startup.
This helps avoid unnecessary memory usage and startup work.
The basic flow is:
.class file
↓
Class Loader
↓
JVM Memory4. Runtime Data Areas
Once classes are loaded, the JVM needs memory areas to store and manage the information required during program execution.
These are called Runtime Data Areas.
The important areas are:
- Method Area
- Heap
- JVM Stack
- Program Counter Register
- Native Method Stack
Some of these areas are shared between threads, while others are created separately for each thread.
Let’s understand them one by one.
5. Method Area
The Method Area stores information related to classes.
It contains information such as:
- Class metadata
- Method information
- Field information
- Runtime constant pool
- Information required for class and method execution
You can think of it as the area where the JVM keeps information about the structure of loaded classes.
For example, if you have:
class Employee {
String name;
int age;
void work() {
System.out.println("Working...");
}
}The JVM needs to maintain information about:
Employee
├── Fields
│ ├── name
│ └── age
│
└── Method
└── work()That class-related information is maintained in the Method Area.
Important Interview Point
In modern JVM implementations such as HotSpot, the Method Area is represented using a memory structure called Metaspace.
6. Heap
The Heap is one of the most important memory areas in Java.
Objects created using new are allocated in the heap.
For example:
Employee employee = new Employee();The Employee object is allocated on the heap.
Conceptually:
Stack Heapemployee ────────────────→ Employee Object
|
├── name
└── ageThe heap is:
- Shared among threads
- Used for object allocation
- Managed by the Garbage Collector
7. Garbage Collection
Java provides automatic memory management through Garbage Collection (GC).
Consider:
Employee employee = new Employee();
employee = null;If the object is no longer reachable through any live reference, it becomes eligible for garbage collection.
The Garbage Collector can eventually reclaim the memory occupied by such objects.
Important:
An object becoming unreachable does not mean it is immediately deleted. It becomes eligible for garbage collection, and the JVM decides when and how garbage collection occurs.
This is one of the major benefits of Java’s automatic memory management.
8. JVM Stack
Each Java thread has its own JVM stack.
The stack stores information associated with method execution.
Whenever a method is called, a stack frame is created.
For example:
public static void main(String[] args) {
calculate();
}
static void calculate() {
int x = 10;
int y = 20;
}Conceptually:
JVM Stack
┌─────────────────────┐
│ calculate() frame │
│ x = 10 │
│ y = 20 │
└─────────────────────┘
│ main() frame │
└─────────────────────┘When calculate() finishes, its stack frame is removed.
Stack Frame
A stack frame contains information associated with a particular method invocation, such as:
- Local variables
- Operand stack
- Reference to the runtime constant pool
- Other method execution information
Because each thread has its own JVM stack, stack data belonging to one thread isn’t directly shared with another thread.
9. Stack vs Heap
This is a very common interview topic.
Stack
- Each thread has its own stack
- Stores stack frames
- Contains local variables and method execution information
- Follows a method-call structure
- Automatically managed as methods enter and exit
Heap
- Shared among threads
- Stores objects and arrays
- Managed by Garbage Collection
- Used for dynamically allocated objects
A simple mental model:
Thread
|
↓
Stack
|
├── main() frame
└── calculate() frame
↓ references
Heap
|
├── Object A
├── Object B
└── Object C10. Program Counter Register
The Program Counter (PC) Register keeps track of the instruction currently being executed by a thread.
In simple terms, it helps the JVM know:
“Which bytecode instruction should this thread execute next?”
Each JVM thread has its own PC Register.
Get engg_nirbhay’s stories in your inbox
Join Medium for free to get updates from this writer.
For example:
Thread 1 → PC Register → Current bytecode instruction
Thread 2 → PC Register → Current bytecode instruction
Thread 3 → PC Register → Current bytecode instructionThis is important because multiple threads can execute different instructions at the same time.
11. Native Method Stack
Java can interact with code written in native languages such as C and C++.
This interaction is commonly achieved using:
JNI — Java Native Interface
When a Java application calls a native method, the JVM uses the Native Method Stack to support execution of native code.
Conceptually:
Java Code
↓
Native Method
↓
JNI
↓
Native Code
↓
C / C++ / OS-level functionality12. Execution Engine
After the classes are loaded and the runtime data areas are prepared, the Execution Engine executes the bytecode.
The Execution Engine primarily involves:
- Interpreter
- JIT Compiler
- Garbage Collection support
Let’s understand the first two.
13. Interpreter
The JVM can initially execute bytecode using an Interpreter.
The Interpreter reads bytecode instructions and executes them.
Conceptually:
Bytecode
↓
Interpreter
↓
Instruction
↓
ExecuteThis approach allows the JVM to start executing code quickly.
However, repeatedly interpreting frequently executed code can be less efficient.
That’s where JIT compilation becomes important.
14. JIT Compiler
JIT stands for Just-In-Time Compiler.
The JIT compiler improves application performance by compiling frequently executed bytecode into native machine code at runtime.
Suppose a method is called thousands or millions of times.
Instead of interpreting the same bytecode repeatedly, the JVM can identify it as hot code and compile it into optimized native machine code.
Conceptually:
Bytecode
↓
Interpreter
↓
Frequently executed code
↓
JIT Compiler
↓
Native Machine Code
↓
Faster ExecutionThis is one of the major reasons Java applications can achieve strong runtime performance.
15. Interpreter vs JIT Compiler
Interpreter JIT Compiler Executes bytecode instruction by instruction Compiles bytecode into native code ,Starts execution quickly Improves performance for frequently executed code. Doesn’t require compilation of the whole method before hand Compiles selected /hot code at runtime Can be slower for repeatedly executed codeFaster after compilation and optimization
The JVM can use both approaches during execution.
16. Native Method Interface
Java sometimes needs to interact with functionality outside the JVM.
For example, Java code may need to interact with:
- Operating system functionality
- Native libraries
- Code written in C/C++
The Java Native Interface (JNI) provides a mechanism for Java code to interact with native code.
Conceptually:
Java Application
↓
JNI
↓
Native Library
↓
C / C++ / OSThis allows Java applications to use native functionality when required.
17. Native Method Libraries
Native Method Libraries are libraries written in native languages such as C or C++ that can be accessed through mechanisms such as JNI.
For example:
Java Code
↓
JNI
↓
Native Library
↓
Operating SystemThese libraries allow the JVM and Java applications to interact with platform-specific functionality.
18. Complete JVM Architecture
Now let’s put everything together.
JVM
|
┌───────────┴───────────┐
↓ ↓
Class Loader Runtime Data Areas
|
┌────────────────────┼───────────────────┐
↓ ↓ ↓
Method Area Heap JVM Stack
|
Stack Frames
|
┌────────────────────┴───────────────────┐
↓ ↓
PC Register Native Method Stack
|
↓
JNI
|
↓
Native Libraries
Runtime Data Areas
↓
Execution Engine
|
┌────────────┴────────────┐
↓ ↓
Interpreter JIT Compiler
│ │
└────────────┬────────────┘
↓
Native Machine Code19. Complete Journey of a Java Program
Let’s trace what happens when we run a Java application.
Step 1 — Write Java Code
HelloWorld.javaStep 2 — Compile
The Java compiler converts the source code into bytecode:
HelloWorld.java
↓
javac
↓
HelloWorld.classStep 3 — Class Loading
The Class Loader loads the required .class files into the JVM.
Step 4 — Runtime Data Areas
The JVM stores different types of information in appropriate memory areas.
- Class-related information → Method Area
- Objects → Heap
- Method calls/local execution data → Stack
- Current instruction → PC Register
- Native method execution → Native Method Stack
Step 5 — Execution
The Execution Engine executes the bytecode.
Initially, the Interpreter may execute the bytecode.
Step 6 — JIT Optimization
Frequently executed code can be compiled by the JIT compiler into optimized native machine code.
Step 7 — Output
The program eventually produces the expected output.
The complete flow:
Java Source Code
↓
javac
↓
Bytecode
↓
Class Loader
↓
Runtime Data Areas
↓
Execution Engine
↓
Interpreter / JIT
↓
Native Machine Code
↓
Execution20. Important JVM Interview Questions
Q1. What is JVM?
JVM is the runtime engine that loads, manages, and executes Java bytecode.
Q2. What is the purpose of the Class Loader?
It loads Java classes into the JVM when they are required.
Q3. Where are objects stored?
Objects are generally allocated in the heap.
Q4. Is the heap shared between threads?
Yes. The heap is shared among JVM threads.
Q5. Is the JVM stack shared between threads?
No. Each thread has its own JVM stack.
Q6. What is a stack frame?
A stack frame is created for each method invocation and contains information required for executing that method.
Q7. What is the purpose of the PC Register?
It keeps track of the current bytecode instruction being executed by a thread.
Q8. What is JIT?
JIT, or Just-In-Time compilation, compiles frequently executed bytecode into native machine code at runtime to improve performance.
Q9. What is JNI?
JNI stands for Java Native Interface. It provides a mechanism for Java code to interact with native code and native libraries.
Q10. What is the difference between Stack and Heap?
Stack is thread-specific and primarily contains method execution data such as stack frames and local variables.
Heap is shared among threads and is primarily used for objects and arrays.
21. Quick Revision
Remember the major JVM components:
Class Loader
↓
Runtime Data Areas
↓
Execution Engine
↓
Native Machine CodeRuntime Data Areas:
Method Area
Heap
JVM Stack
PC Register
Native Method StackExecution Engine:
Interpreter
+
JIT CompilerNative interaction:
Java
↓
JNI
↓
Native LibrariesFinal Takeaway
The JVM is much more than just a program that “runs Java code.”
It is responsible for:
- Loading classes
- Managing memory
- Creating and managing runtime data
- Executing bytecode
- Performing JIT compilation
- Garbage collection
- Supporting multithreading
- Interacting with native code
The most important mental model to remember is:
Java Program
↓
Bytecode
↓
Class Loader
↓
Runtime Data Areas
↓
Execution Engine
↙ ↘
Interpreter JIT
↘ ↙
Native Code
↓
CPU / OSOnce you understand this flow, concepts such as heap vs stack, garbage collection, class loading, JIT compilation, memory leaks, OutOfMemoryError, and StackOverflowError become much easier to understand.
Java code is compiled into bytecode, but the JVM is responsible for turning that bytecode into actual execution.
This is why understanding JVM internals is essential for becoming a strong Java developer and for answering Java interview questions with confidence.
Java Interview Prep Series #4 — JVM Internals




