How Does a Java Program Run Inside the JVM? A Step‑by‑Step Walkthrough
This article demystifies the JVM’s runtime data areas and illustrates, with a simple Java example and animation, how a program’s bytecode is executed—from the program counter and stack frames to heap allocation and garbage collection—providing a clear, step‑by‑step view of Java execution.
We all know Java programs run on the JVM, but many are unaware of how the execution actually proceeds. This article explores that process.
Program Counter: The bytecode execution indicator that records the current line number, private to each thread.
JVM Stack: Also thread‑private, it provides a stack frame for each method, storing the local variable table, method return address, etc. When a method is invoked, a frame is pushed; when it returns, the frame is popped.
Native Method Stack: Similar to the JVM stack but serves native (C/C++) methods.
Heap: The largest memory area in the JVM, storing objects; garbage collection primarily targets this region.
For developers, the heap and stack are the most relevant parts of the JVM because they hold objects and their references.
Consider the following simple code:
public class Example {
public static void main(String[] args) {
a();
}
public static void a() {
int a = 1;
b();
}
public static void b() {
User b = new User();
}
}The code defines a class with a main() method that calls a(), which defines an int variable and calls b(); b() creates a new User object.
To understand the execution, you need a bit of prior knowledge:
The local variable table in a stack frame stores Java primitive and reference types.
Instance objects reside in the heap, while the stack holds references to those instances.
The following steps illustrate how the above code runs inside the JVM:
Program starts.
main() method is pushed onto the stack.
a() is invoked and pushed.
Primitive variable a is created in the stack.
b() is invoked and pushed.
Reference variable b is created in the stack.
An instance of User is allocated in the heap.
The heap instance’s address is stored in the reference b.
b() finishes, b is popped and the reference is cleared.
a() finishes, a is popped.
Garbage collection reclaims the now‑unreferenced User object.
main() finishes and is popped.
Program execution ends.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java High-Performance Architecture
Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
