JVM Architecture and Lifecycle: Class Loading, Memory Areas and a Worked Bytecode Trace

Follow one Java program from its class files through loading, linking, initialization, stack frames, heap reachability and JVM shutdown.

KnowledgeGate Team

Exam prep & CS education

Updated 28 Nov 20266 min read
JVM Architecture and Lifecycle: Class Loading, Memory Areas and a Worked Bytecode Trace

JVM architecture diagrams often leave you with disconnected boxes: class loader, heap, stacks, execution engine and native code. You may label every box correctly yet still struggle to follow a program from class preparation to process shutdown. The earlier JVM Architecture in Java: Class Loading, Memory Areas and a Bytecode Trace traces an Account from source through the complete subsystem map. Counter adds a different boundary: preparation sets base to 0, initialization changes it to 10, add(7) returns 34, reachability changes, and live non-daemon threads determine when the process can shut down.

Put the JVM in the Java execution path

The route is JVMTrace.java + Counter.java -> javac -> JVMTrace.class + Counter.class -> compatible JVM implementation -> execution on the host. A class file is a binary format containing JVM instructions and supporting metadata. It is not native machine code for one processor.

Keep three terms separate. The JDK supplies development tools such as a compiler, along with runtime components. A Java runtime supplies libraries and the machinery required to run compatible code. The JVM is the abstract execution machine defined by the JVM Specification and realised by an implementation. Vendors do not have to package a separate product called a JRE in the same way.

Portability has a boundary. A compatible class file can run through conforming JVM implementations on different hosts, but each implementation still depends on host-specific machine code, operating-system services and native libraries.

Read JVM architecture as responsibilities and ownership

The Java SE 25 JVM Specification, Chapter 2 defines the ownership rules. The heap and method area are shared. Every thread owns a pc register and JVM stack. A frame holds locals, an operand stack and a reference to the current class's run-time constant pool.

Area or subsystem

Job

Shared or per-thread

Worked value

Class loading system

Creates class representations

JVM-wide activity

Counter

Method area

Holds type data, bytecodes and constant pools

Shared

Counter data, base = 10

Heap

Holds objects

Shared

Counter@C1

pc register

Identifies the current instruction

Per-thread

Next add bytecode

JVM stack

Holds frames

Per-thread

main and add frames

Native-method stack

Supports native methods when needed

Per-thread facility

None here

Execution mechanism

Consumes or compiles bytecodes

JVM-wide activity

Executes add

Interpreter, JIT compiler, garbage collector, JNI and native libraries are implementation machinery around the abstract machine. No single internal layout, compilation threshold or collection algorithm is required.

JVM map: the shared method area holds base = 10, the heap holds Counter@C1, and one thread stack holds the main and add frames.

Use one program to separate class initialization from object construction:

java
class Counter {
    static int base = initialiseBase();

    static int initialiseBase() {
        System.out.println("class-init");
        return 10;
    }

    Counter() {
        System.out.println("object-init");
    }

    int add(int x) {
        int y = x + base;
        return y * 2;
    }
}

public class JVMTrace {
    public static void main(String[] args) {
        System.out.println("main-start");
        Counter c = new Counter();
        int result = c.add(7);
        System.out.println(result);
    }
}

Loading creates the JVM representation of Counter from its binary form. Linking includes verification, preparation and resolution. Preparation gives static int base its default value 0. Resolution of symbolic references may be lazy, so it need not happen in one eager batch.

Before the first active use creates a Counter, initialization evaluates the static initializer, prints class-init, and changes base to 10. Allocation then creates Counter@C1, supplies default values for its instance state, and runs the constructor, which prints object-init. The visible output is main-start, class-init, object-init, then 34, one per line. An implementation may load or link Counter earlier, but JVM Specification Chapter 5 and JLS Chapter 12 preserve the required initialization ordering.

Counter class lifecycle in five stages, where prepare sets base = 0 and initialization runs initialiseBase() to produce base = 10.

Worked bytecode and frame trace: add(7) returns 34

At the call site, Counter@C1 exists and base = 10. The main frame holds c -> Counter@C1. Calling c.add(7) creates an add frame with slot 0 = this -> Counter@C1, slot 1 = x = 7, and slot 2 reserved for y.

In this table, [] means empty and the rightmost value is on top:

Instruction

Operand stack change

Other effect

iload_1

[] -> [7]

Load x

getstatic Counter.base

[7] -> [7, 10]

Read base

iadd

[7, 10] -> [17]

Add integers

istore_2

[17] -> []

Set local y = 17

iload_2

[] -> [17]

Load y

iconst_2

[17] -> [17, 2]

Push 2

imul

[17, 2] -> [34]

Multiply

ireturn

[34] -> [] in the callee

Deliver 34 to the caller

The audit is y = 7 + 10 = 17, then result = 17 x 2 = 34; println prints 34. The trace illustrates this source shape. Exact offsets and constant-pool indices are compiler artefacts. The earlier JRE and JVM Explained: How Java Loads and Executes Bytecode uses FareTable to isolate the JDK/JRE boundary and first-use initialization. Counter continues past that point into object construction, reachability and process shutdown.

Execution, JIT compilation, native calls and garbage collection

The invariant is that a JVM implementation must preserve the program-visible semantics of eligible class files. It may interpret instructions, compile selected methods to native code, or mix both. A JIT is not a required class-lifecycle phase, and no universal call count triggers compilation.

While local c points to Counter@C1, the object is reachable. If we add c = null after the calculation, result still contains the primitive value 34. If no other live reference reaches Counter@C1, its heap storage may later be reclaimed. Setting the reference to null does not immediately run a collector, destroy the class or reset static base to 0.

Stack exhaustion and heap exhaustion are different failures. Garbage collection concerns reclaimable heap storage, not normal frame popping. JNI is a bridge for native methods, not the path followed by every Java method.

JVM process lifecycle: startup, live threads and shutdown

One JVM process starts by selecting an initial class, initializing it as required, invoking the entry method and creating threads. Launcher details belong to a particular tool, not the abstract JVM contract.

Suppose main creates non-daemon worker W with exactly two queued jobs, Q1 and Q2, plus daemon helper D, then returns. The JVM stays alive while W is live. After W finishes both jobs, D alone cannot keep it alive. Shutdown can begin if no other non-daemon thread is live.

The Java SE 25 Runtime API says shutdown begins when live non-daemon threads fall to zero or an exit operation starts it. Hooks begin in unspecified order and run concurrently. Termination follows the sequence, while halt is abrupt. Never assume hook order, daemon-task completion, object finalization or clean shutdown after every crash.

How exams and interviews can test JVM architecture

Stable practice forms include ordering the phases, classifying shared and per-thread areas, tracing an operand stack, separating class initialization from construction, and deciding whether a daemon thread keeps a JVM alive. This is not an official paper pattern.

Repair these common traps:

  • base becomes 10 during preparation: preparation gives 0; initialization produces 10.

  • Local c stores Counter@C1 itself: c stores a reference to the object.

  • Each object owns a JVM stack: each thread owns a JVM stack.

  • Method area equals call stack: type data and frames have different homes.

  • JIT is a required lifecycle phase: it is an implementation strategy.

  • c = null destroys Counter@C1 immediately: collection timing is not guaranteed.

  • Main returns, so the JVM always exits: live non-daemon W can keep it alive.

For a 45-second routine, label each noun class, object, thread, frame or process. Write 0 -> 10, 7 + 10 = 17, and 17 x 2 = 34, then answer from ownership and phase order. Object Oriented Technology Explained: Four Pillars in Java provides the class and object foundation.

The short version and the next study step

Class files carry JVM instructions. Loading precedes linking and initialization. The heap and method area are shared; stacks and pc registers belong to threads. Frames use locals and an operand stack. Non-daemon threads affect shutdown.

Check the trace once more: base: 0 -> 10, x = 7, y = 17, result = 34. Redraw both diagrams and explain why class initialization precedes construction of Counter@C1. Continue with the Java Course for structured language study and DSA using Java for placement-focused problem solving.