How does the JVM work in detail?
How does the JVM (Java Virtual Machine) work in detail?
2-Line Answer
The JVM is the engine that converts Java bytecode into machine code so your program can run on any operating system. Along the way, it loads classes, manages memory, executes code, optimizes performance, and automatically cleans up unused objects.
Detailed Answer
If you've ever heard the phrase "Write Once, Run Anywhere," the JVM is the reason it works.
When you write Java code, the computer doesn't understand it directly. Your code first goes through the Java compiler, which converts it into bytecode. The JVM then reads that bytecode and turns it into machine code that your operating system can understand.
Let's walk through the entire process from start to finish.
Step 1. Write Java Code
Everything starts with a Java source file.
This is the code you write every day.
Humans can read it.
Computers can't.
Step 2. Compilation
When you run the compiler,
This .class file contains bytecode, not machine code.
That's why the same .class file can run on Windows, Linux, or macOS.
Step 3. Class Loader
Now the JVM starts.
The first component it uses is the Class Loader.
Its job is to find and load the required .class files into memory.
For example, if your code uses
The JVM doesn't just load your HelloWorld.class.
It also loads classes like:
- String
- Object
- System
- PrintStream
The Class Loader makes sure every required class is available before execution begins.
Step 4. Bytecode Verification
Before running the code, the JVM performs a security check.
It verifies that the bytecode is safe.
For example, it checks:
- Is the bytecode valid?
- Is memory being accessed correctly?
- Are method calls legal?
If something looks suspicious or corrupted, the JVM refuses to execute it.
This protects your system from invalid or malicious bytecode.
Step 5. Runtime Data Areas
Once verification is complete, the JVM creates memory areas.
Each area has a specific responsibility.
Method Area
Stores:
- Class metadata
- Method information
- Static variables
- Runtime constant pool
This data is shared by all threads.
Heap Memory
This is where objects live.
Whenever you write
the object is created inside the Heap.
The Heap is also shared by all threads.
Stack Memory
Every thread gets its own Stack.
Whenever a method is called, the JVM creates a Stack Frame.
That frame stores:
- Local variables
- Method parameters
- Partial results
When the method finishes, its frame is removed automatically.
Program Counter (PC Register)
Each thread keeps track of the next instruction to execute.
Think of it as the JVM's bookmark.
Whenever one instruction finishes, the PC Register points to the next one.
Native Method Stack
Sometimes Java needs to call code written in C or C++.
Those native methods use a separate Native Method Stack.
Step 6. Execution Engine
Now the JVM is ready to execute your code.
The Execution Engine has two important parts.
Interpreter
The Interpreter reads bytecode one instruction at a time.
It's quick to start but slower for repeated execution.
Imagine reading a book and translating every sentence while reading.
That's exactly how the Interpreter works.
JIT Compiler (Just-In-Time Compiler)
Now imagine the JVM notices that one method is being called thousands of times.
Instead of interpreting it repeatedly, the JVM compiles that bytecode directly into machine code.
The next time the method runs, the CPU executes the machine code immediately.
That's much faster.
This is one of the biggest reasons Java applications become faster after running for a while.
Step 7. Garbage Collector
Objects don't stay in memory forever.
Once an object is no longer being used, it becomes eligible for garbage collection.
The Garbage Collector automatically removes these unused objects.
This frees Heap memory and prevents memory leaks caused by forgotten objects.
Developers don't need to manually free memory like they do in C or C++.
Step 8. Native Method Interface (JNI)
Some applications need to interact with operating system libraries.
For example,
- Camera drivers
- Printer drivers
- Hardware communication
- Existing C/C++ libraries
Java uses the Java Native Interface (JNI) to communicate with native code.
Putting Everything Together
Here's the complete journey.
This entire process happens every time you run a Java application.
Real-World Example
Imagine you're watching a movie in a foreign language.
The movie is your Java bytecode.
The translator is the JVM.
At first, the translator translates every sentence one by one.
That's the Interpreter.
After hearing the same sentence hundreds of times, the translator memorizes it.
Now the sentence is spoken instantly without translating it again.
That's exactly what the JIT Compiler does.
Meanwhile, someone in the theater keeps removing empty popcorn boxes and water bottles.
That's the Garbage Collector, cleaning up memory while the movie continues.
Code Example
🚀 Crack Your Next Java Interview
Reading articles is great, but consistency is what gets you hired. I drop one real, company-tested Java interview question every single day on Instagram.
No fluff—just the exact questions tech panels are asking right now, broken down simply.
👉
Comments
Post a Comment