Execution Engine
Execution Engine Phase
Once .class files are loaded, linked, and initialized, the JVM moves into execution phase.
Execution Engine is the place Where Code actually Runs
It contains:-
- Interpreter
- JIT (Just in Time)
- Garbage Collector
1. Interpreter
Interpreter reads bytecode one instruction at a time and executes it immediately.
Interpreter works like:
- Read instruction 1 → execute
- Read instruction 2 → execute
- Read instruction 3 → execute
Very fast startup, No waiting for compilation and Slower for long-running programs
2. JIT Compiler (Just-in-time)
- Detects hot methods(frequently executed)
- Converts bytecode - native machine code
- Stores optimized code for reuse
- It results, Much faster execution and JVM becomes faster over time
What are “Hot Methods”?
- Methods inside loops
- Frequently called methods
- Business logic methods in servers
Why JIT makes JVM faster
- Once compiled:
- JVM does not interpret again
- CPU runs native machine code directly
- Execution becomes much faster
When you run a Java program, JVM does three big things:
- Loads the .class files
- Prepares them (linking + initialization)
- Executes the code
Real objects are created only during the Execution Phase, when the new keyword is actually executed by the Execution Engine.
Step by Step
1. Compile Time
User u = new User();
- It convert .java file into .class
No object is created here
2. Class Loading / Linking / Initialization
- JVM loads User.class
- Prepares class metadata
- Initializes static variables
- Static variables initialized
Object NOT created yet
3. Execution
- When JVM executes this line:
- User u = new User();
Now the real object is created
What Exactly Happens When new Executes?
- Internally:
- JVM checks: Is User class loaded?
- If not → load it
- JVM allocates memory in Heap
- Object fields are initialized
- Constructor is called
- Reference u is stored in Stack
Stack Heap
u ─────────▶ User object
Now the real object exists
3. Garbage Collector
- Automatically removes unused objects from Heap
- Frees memory
- Prevents memory leaks
GC types:
- Minor GC → Young Gen
- Major/Full GC → Old Gen
Native Libraries
- File IO
- Threading
- Memory allocation
➡️ JVM calls Native Libraries using JNI
Garbage Collector :
Comments
Post a Comment