JVM Architecture in Java: Class Loading, Memory Areas and a Bytecode Trace
Trace a runnable Java program from source to class files, class initialization, run-time memory areas and selected bytecode instructions.
KnowledgeGate Team
Exam prep & CS education

A Java file does not execute directly, and a box labelled “JVM” does not show where bytecode, objects, method calls and optimisation fit. That gap makes architecture diagrams hard to apply in code-tracing questions under exam or interview pressure. Account moves from JvmTraceDemo.java through javac, class loading, run-time areas and selected bytecode instructions, with values 100, 325, 875 and 101 tracked at each stage. If you first need the broader learning route, start with the Coding & DSA Courses for Placements category.
JVM architecture: follow the program, not a memorised box diagram
The JDK supplies tools such as javac and the java launcher. A JVM is the abstract machine that loads and executes class-file code; run-time libraries support the program. JDK, JVM and a separately installed JRE are not interchangeable.
Here, javac turns JvmTraceDemo.java into JvmTraceDemo.class and Account.class. The JVM loads, links and initializes them; logical run-time areas hold frames and objects; the execution engine processes bytecode and prints two lines.
Components include class loading; verification, preparation, resolution and initialization; the method area, heap, JVM stacks, per-thread pc registers and native method stacks; interpreted or compiled execution; garbage collection; and native integration. Oracle's Java Virtual Machine Specification, Java SE 25, Chapter 2 defines required behaviour. HotSpot chooses implementation details, not a prescribed physical layout.

Compile and run one complete Account example
Save this copy-paste program as JvmTraceDemo.java:
class Account {
static int nextId = 100;
final int id;
final String owner;
int balance;
Account(String owner, int balance) {
this.id = nextId++;
this.owner = owner;
this.balance = balance;
}
int debit(int amount) {
balance = balance - amount;
return balance;
}
}
public class JvmTraceDemo {
public static void main(String[] args) {
Account account = new Account("Asha", 1200);
int fee = 75;
int left = account.debit(250 + fee);
System.out.println(account.owner + "#" + account.id
+ " balance=" + left);
System.out.println("nextId=" + Account.nextId);
}
}Compile and run it:
javac JvmTraceDemo.java
java JvmTraceDemoThe constructor assigns id = 100 and advances nextId to 101. Then 250 + 75 = 325, and 1200 - 325 = 875.
Asha#100 balance=875
nextId=101Two top-level class declarations produce two class files. Sizes, constant-pool indexes and bytecode offsets can vary by compiler and release.
Class loading, linking and initialization with actual values
Under Oracle's Java Virtual Machine Specification, Java SE 25 edition, Chapter 5, loading obtains a binary representation and creates the JVM class or interface representation. Linking covers verification, preparation and resolution. These respectively check structural and type-safety constraints, create class variables with defaults, and convert symbolic references into direct run-time references when chosen.
Preparation gives Account.nextId the int default 0; class initialization applies the explicit 100. The first constructor's nextId++ stores 100 in account.id and leaves nextId=101. Loading alone does not run the initializer. The entry class must be ready for main, while active use through new Account(...) requires Account initialization before construction.
Put each worked value in the correct run-time data area
Area | Shared or per-thread | Worked contents | Lifetime |
|---|---|---|---|
Method area | Shared | Class structures, code and run-time constant pools | While classes remain loaded |
Heap | Shared |
| Until unreachable and reclaimed |
JVM stack | Per-thread |
| Frames follow calls; stack follows thread |
| Per-thread | Current JVM instruction for a non-native method | Thread lifetime |
Native method stack | Per-thread | Native-method support where used | Thread lifetime |
The main frame holds args, account, fee=75 and later left=875. debit(325) adds a frame where this points to the same heap object, amount=325, and the operand stack computes the result. Returning 875 pops it and fills left. Frames also refer to the current class's run-time constant pool. The specification fixes no physical address for nextId.

Execution engine, JIT compilation, garbage collection and native calls
The execution engine processes JVM instructions. A JVM may interpret code and compile selected methods or regions to native code. Selection, thresholds and tiering are implementation-specific, so debit has no portable compilation point.
Garbage collection follows reachability. Local account keeps the object reachable during main; afterwards it can become eligible, but eligibility and System.gc() promise no immediate reclamation. Not every OutOfMemoryError means heap exhaustion.
Native-method stacks support JNI and libraries. This program declares no native method; ordinary bytecode does not inherently cross JNI.
Read selected bytecode with javap -c
Run javap -c JvmTraceDemo and javap -c Account. In main, find new Account, dup, loads of "Asha" and 1200, invokespecial Account.<init>, reference storage, the push of 75, then the receiver and argument for invokevirtual Account.debit.
Follow the stack effects: push 250, load fee=75, then iadd produces 325. In debit, getfield balance produces 1200; load amount=325; isub produces 875; putfield stores it; then a second getfield balance reloads the field before ireturn sends 875 to main. The meanings follow Oracle's Java Virtual Machine Specification, Java SE 25 edition, Chapter 6. Bytecode is stack-oriented despite source variable names.
String concatenation may use invokedynamic, so no println sequence is universal. Java string handling, the string pool and immutability covers the distinct String topic. A run-time constant pool is not the String pool.
Common JVM architecture traps in exams and interviews
Correct four broken models:
JDK = JVMignores the JDK's other tools.Loaded = initializedignores linking and initialization.“Every reference is on the heap” ignores references held in frames.
“JIT compiles everything before execution” is not portable.
For a trace, mark shared versus per-thread areas, separate class and instance fields, apply preparation defaults before initializers, push frames, calculate operand stacks in order, and require no live root before collection. Here: shared heap and method area; per-thread stack and pc; nextId: 0 -> 100 -> 101; result 875.
Unbounded recursion can cause StackOverflowError; failed allocation can cause OutOfMemoryError; bad class data can fail during loading or linking. Fixed depths, sizes, JIT thresholds and collectors require implementation details and flags.
Exercises, the short version and your next Java step
Try these before checking the answers:
Add
Account second = new Account("Vikram", 900). Predict its ID andnextId.Change
feefrom75to100. Predict the argument and balance.Classify class metadata, the object,
debitframe and instruction pointer.
Answers: A gives second.id=101, nextId=102. B gives 250 + 100 = 350, then 1200 - 350 = 850. C gives method area, heap, the calling thread's JVM stack and its pc register. For basic compile-run practice, use classic programs in C, Java and Python.
The short version: javac creates class files. The JVM loads, links and initializes classes. Shared and per-thread areas hold class structures, objects and frames. The engine interprets or compiles bytecode under implementation-specific policies. Garbage collection reclaims unreachable heap objects without a promised time.
Compile the file, run both javap -c commands, and mark where 325, 875 and 101 arise. Then try over 20 JVM architecture and lifecycle questions. Continue with Java: Concepts, MCQs and Coding Questions, followed by DSA using Java for data-structure and interview problems.
Keep learning
JVM Architecture and Lifecycle MCQs: 12 Solved Java Questions with Explanations
Solve 12 published JVM questions, then use the explanations to separate bytecode, loading, execution, memory areas, reachability and runtime type checks.
Java Memory Model Tutorial: Happens-Before, volatile and Safe Publication
Learn the Java Memory Model through exact start/join, volatile, synchronized, final-field and AtomicInteger examples, with clear happens-before guarantees.
Classes and Objects in Java: Build and Trace Two Objects Step by Step
Build a Java Student class, create two independent objects, and trace fields, methods, aliases, null references, output, and common errors step by step.
Kotlin vs Java for JVM Applications: A Clear Trade-off Guide
Choose between Kotlin and Java using the same Ticket #42 service example, a migration checklist, and a scorecard whose winner changes when team priorities change.





