Back to Blog

Inside the JVM: Class Loaders, Runtime Areas and Execution Engine

Before a Java program executes, something interesting happens beneath the surface. The JVM takes compiled bytecode, loads the required classes, organizes memory, manages execution, and eventually translates frequently executed instructions into native machine code.

We have already explored how Java source code is compiled into bytecode and how the JDK, JRE, and JVM fit into the Java ecosystem.

Now, let's look inside the JVM itself.

Understanding its internal architecture helps us understand not only how Java works, but also why Java applications behave the way they do in terms of memory management, performance, concurrency, and garbage collection.

1. What Happens Inside the JVM?

The JVM is more than an environment that executes .class files. It is a runtime system responsible for managing the complete lifecycle of Java bytecode execution.

At a high level, the JVM can be understood through three major components:

  1. Class Loader Subsystem – Loads Java classes into the JVM.
  2. Runtime Data Areas – Organizes memory required during execution.
  3. Execution Engine – Executes bytecode and optimizes its performance.

The JVM also interacts with native libraries and operating-system resources through the Java Native Interface (JNI).

Let's visualize the architecture.

             Java Source Code
                    |
                    v
             Java Compiler
                 (javac)
                    |
                    v
               Bytecode
                (.class)
                    |
                    v
        +-----------------------+
        | Class Loader Subsystem|
        |                       |
        | Loading               |
        | Linking               |
        | Initialization        |
        +-----------------------+
                    |
                    v
        +-----------------------+
        |   Runtime Data Areas  |
        |                       |
        | Heap                  |
        | Method Area           |
        | Java Stacks           |
        | PC Registers          |
        | Native Method Stacks  |
        +-----------------------+
                    |
                    v
        +-----------------------+
        |    Execution Engine   |
        |                       |
        | Interpreter           |
        | JIT Compiler          |
        | Garbage Collector     |
        +-----------------------+
                    |
                    v
             Native Execution
                    |
                    v
             Operating System

This is a conceptual representation. The actual JVM implementation involves more detailed interactions between these components.

2. Class Loader Subsystem: How Java Classes Enter the JVM

Imagine executing a Java application.

public class Application {
    public static void main(String[] args) {
        UserService service = new UserService();
        service.process();
    }
}

The JVM cannot execute a class simply because its .class file exists somewhere on the filesystem.

It first needs to load the class into memory and make it available for execution.

This responsibility belongs to the Class Loader Subsystem.

The three stages of class loading

The class-loading process consists of three major stages:

1. Loading

The class loader locates the binary representation of a class and reads its bytecode.

For example:

Application.class
UserService.class

The JVM creates an internal representation of the loaded class.

2. Linking

Linking prepares the loaded class for execution. It consists of three stages:

  • Verification: Checks whether the bytecode follows JVM structural and safety rules.
  • Preparation: Allocates memory for static fields and assigns their default values.
  • Resolution: Converts symbolic references in the runtime constant pool into direct references where required.

3. Initialization

During initialization, static initializers and explicit static field assignments are executed.

For example:

public class Application {

    static int count = 10;

    static {
        System.out.println("Class initialized");
    }
}

The initialization process executes the static initialization logic when the JVM determines that initialization is required.

An important distinction: loading, linking, and initialization are separate operations. A class can be loaded without being initialized.

Types of Class Loaders

Java provides a hierarchy of built-in class loaders.

1. Bootstrap Class Loader

This is the foundational class loader.

It loads essential Java platform classes, such as classes from the java.lang and java.util packages.

For example:

String name = "Java";

The String class is part of the Java platform and is loaded through the bootstrap mechanism.

2. Platform Class Loader

The platform class loader loads platform classes that are not loaded by the bootstrap class loader.

It replaced the older Extension Class Loader role in modern Java.

3. Application Class Loader

Also known as the System Class Loader, it loads application classes from the application classpath or module path.

For example:

public class UserService {
    public void process() {
        System.out.println("Processing...");
    }
}

An application class like UserService is generally loaded by the application class loader.

Parent Delegation Model

Java class loaders generally follow a parent-delegation model.

When a class loader receives a request to load a class, it first delegates the request to its parent. If the parent cannot load it, the requesting loader attempts to load it itself.

       Bootstrap Class Loader
                 |
                 v
        Platform Class Loader
                 |
                 v
       Application Class Loader
                 |
                 v
        Custom Class Loaders

Why does Java use this model?

Consider a situation where someone creates another class named java.lang.String.

Without appropriate loading restrictions, an application might attempt to replace a fundamental Java platform class.

Parent delegation helps ensure that platform classes are loaded by trusted parent loaders rather than arbitrary application class loaders.

Custom class loaders are also possible. They are useful in application servers, plugin architectures, frameworks, and environments where classes need to be loaded dynamically.

An interesting detail: In Java, class identity is determined not only by its fully qualified name but also by its defining class loader. Two classes with the same name loaded by different defining class loaders can represent different types to the JVM.

3. JVM Runtime Data Areas: Where Memory Is Organized

Once classes are loaded, the JVM needs memory to execute instructions, store objects, maintain method calls, and manage application state.

This is where Runtime Data Areas come into the picture.

The JVM specification defines several runtime memory areas.

Some are shared across threads, while others are created separately for each thread.

Let's understand them individually.

3.1 Heap

The Heap is the primary memory area where Java objects and arrays are allocated.

Consider:

User user = new User();

user.setName("Arko");

Here:

  • The User object is generally allocated on the heap.
  • The reference variable user is stored in the stack frame of the executing method if it is a local variable.
  • The object's fields are part of the object stored on the heap.

The Heap is shared among application threads.

It is also the primary area managed by the Garbage Collector.

For example:

User user = new User();

user = null;

If no reachable reference points to the original object, it may eventually become eligible for garbage collection.

However, becoming eligible for garbage collection does not mean that the object is immediately removed.

3.2 Method Area

The Method Area is a logical memory area defined by the JVM specification.

It stores class-level structures, including:

  • Class metadata.
  • Runtime constant pools.
  • Method and field information.
  • Method bytecode.
  • Static variables and related class-level information, depending on JVM implementation.

In HotSpot JVM, much of the class metadata is stored in a memory region called Metaspace.

Metaspace was introduced in Java 8, replacing the older Permanent Generation (PermGen) implementation.

An important distinction:

The Method Area is a JVM specification concept, whereas Metaspace is a HotSpot implementation detail.

They are related, but they are not interchangeable terms.

3.3 Java Stack

Every Java thread has its own Java Virtual Machine Stack.

Whenever a method is invoked, the JVM creates a stack frame for that method.

A stack frame contains information such as:

  • Local variables.
  • Operand stack.
  • Runtime constant pool references needed by the method.
  • Method return information.

Consider:

public class Calculator {

    public static void main(String[] args) {
        int result = add(10, 20);
        System.out.println(result);
    }

    static int add(int a, int b) {
        return a + b;
    }
}

Conceptually, the execution looks like this:

        Java Stack
    -------------------

    main() Stack Frame
    -------------------
    args
    result

    add() Stack Frame
    -------------------
    a = 10
    b = 20

    -------------------

When add() is called, a new stack frame is pushed onto the stack.

When the method finishes, its frame is popped.

This is why Java method calls naturally follow the Last In, First Out (LIFO) principle.

Recursive method calls also create additional stack frames.

If recursion becomes excessively deep, the application may encounter:

java.lang.StackOverflowError

3.4 PC Register

Every JVM thread has its own Program Counter (PC) Register.

It keeps track of the address of the JVM instruction currently being executed, or the next instruction to execute, depending on the JVM's execution state.

Why does each thread need its own PC register?

Because multiple threads can execute independently.

Each thread needs to maintain its own execution position so that the JVM can switch between threads and resume execution correctly.

For a thread executing a Java method, the PC identifies the current JVM instruction.

When executing a native method, its value is undefined in the JVM specification.

3.5 Native Method Stack

Java applications sometimes need to interact with native code written in languages such as C or C++.

For example, a Java application may need to interact with operating-system functionality or an existing native library.

The JVM provides the Java Native Interface (JNI) for this purpose.

Native Method Stacks support the execution of native methods.

Their exact implementation and memory organization depend on the JVM.

Shared vs Thread-Specific Memory

Let's summarize the runtime data areas.

Memory Area Scope
Heap Shared
Method Area Shared
Java Stack Per thread
PC Register Per thread
Native Method Stack Per thread

This distinction becomes particularly important when studying multithreading and concurrency.

Two threads can share objects in the Heap while maintaining separate method execution stacks.

This is one of the fundamental reasons why concurrent programming requires careful synchronization and thread-safety considerations.

4. Execution Engine: How Bytecode Becomes Execution

Now we reach the component responsible for executing Java bytecode.

The Execution Engine reads the instructions loaded into the JVM and executes them.

It primarily involves three important mechanisms:

  1. Interpreter.
  2. Just-In-Time (JIT) Compiler.
  3. Garbage Collector.

4.1 Interpreter

The Interpreter executes bytecode instructions.

It reads instructions sequentially and performs the corresponding operations.

For example:

int a = 10;
int b = 20;
int result = a + b;

The Java compiler converts this source code into bytecode instructions.

The interpreter executes those instructions inside the JVM.

The advantage of interpretation is that execution can begin without compiling the entire application into native machine code first.

However, repeatedly interpreting frequently executed code can introduce performance overhead.

This is where the JIT Compiler becomes important.

4.2 Just-In-Time (JIT) Compiler

Java applications often execute the same methods repeatedly.

For example, consider an application processing thousands of customer transactions.

Some methods may be called millions of times.

Interpreting the same bytecode repeatedly is not always the most efficient approach.

The JIT Compiler addresses this problem.

It identifies frequently executed code, often called hot code, and compiles it into native machine code.

The compiled code can then be executed directly by the processor rather than being interpreted instruction by instruction.

Conceptually:

          Java Bytecode
                |
                v
            Interpreter
                |
                v
       Execution Profiling
                |
                v
        Frequently Executed
              Methods
                |
                v
           JIT Compiler
                |
                v
        Native Machine Code
                |
                v
                CPU

Modern HotSpot JVMs use tiered compilation.

They can use different compilation levels, allowing the JVM to balance startup performance with optimized execution.

For example:

  • C1 compiler: Focuses on relatively fast compilation.
  • C2 compiler: Focuses on more aggressive optimization for frequently executed code.

The JVM can also perform optimizations such as method inlining, loop optimizations, and escape analysis.

This explains one of Java's major performance advantages.

Java does not simply interpret every instruction forever. It can adapt its execution strategy based on how the application behaves at runtime.

4.3 Garbage Collector

Memory management is another important responsibility of the JVM.

In languages where developers manage memory manually, they must explicitly allocate and release memory.

Java provides automatic memory management through Garbage Collection (GC).

Consider:

public void process() {

    User user = new User();

    user.setName("Arko");

}

After the method finishes, if no reachable reference points to the created object, the object may become eligible for garbage collection.

The Garbage Collector identifies objects that are no longer reachable from the relevant GC roots and reclaims their memory.

GC roots can include:

  • Active thread stacks.
  • Static references.
  • JNI references.
  • Other JVM-maintained references.

Garbage Collection helps reduce manual memory-management complexity, but it does not eliminate memory-related problems.

Java applications can still experience:

  • Memory leaks.
  • Excessive object allocation.
  • High garbage collection overhead.
  • OutOfMemoryError.

Understanding GC is particularly important when building large-scale backend applications.

5. Putting Everything Together: A Java Program's Journey

Let's connect all these concepts using a simple example.

public class Application {

    public static void main(String[] args) {

        User user = new User();

        user.setName("Arko");

        System.out.println(user.getName());
    }
}

What happens when we execute this program?

Step 1: Class Loading

The Application class is loaded by the application class loader.

When the JVM needs the User class, it is also loaded.

Step 2: Linking and Initialization

The JVM verifies the classes, prepares their runtime structures, resolves references as necessary, and performs initialization when required.

Step 3: Memory Allocation

The JVM allocates memory for the objects and maintains the required class-level structures.

The User object is generally allocated on the Heap.

Step 4: Method Execution

The JVM creates a stack frame for the main() method.

When setName() and getName() are called, their method execution frames are managed through the Java Stack.

Step 5: Bytecode Execution

The Execution Engine executes the bytecode using interpretation and, where appropriate, JIT compilation.

Step 6: Memory Management

When objects are no longer reachable, they may become eligible for garbage collection.

The JVM manages the reclamation of their memory.

This entire process happens behind a relatively simple command:

java Application

That simplicity is one of the most interesting aspects of Java.

Developers write application logic while the JVM manages much of the complexity underneath.

6. Why Understanding JVM Internals Matters to a Java Developer

It is easy to write Java applications without understanding how the JVM works internally.

Frameworks such as Spring Boot, Hibernate, and application servers abstract many of these details.

However, understanding the JVM becomes increasingly important as applications grow.

Consider a few practical scenarios:

Problem JVM Concept
StackOverflowError Java Stack
OutOfMemoryError: Java heap space Heap
OutOfMemoryError: Metaspace Class metadata / Metaspace
Slow application startup Class loading and initialization
High CPU usage Execution and JIT optimization
Frequent GC pauses Garbage Collection
Class loading conflicts Class Loader Subsystem
Thread-related memory issues Thread-specific runtime areas

For example, when investigating a production performance issue, understanding the JVM helps us ask better questions:

  • Is the application allocating too many objects?
  • Is the Heap appropriately sized?
  • Is garbage collection consuming excessive CPU time?
  • Are classes being loaded repeatedly?
  • Is the JIT compiler optimizing frequently executed methods?
  • Are too many threads consuming memory?

These are not merely theoretical questions. They are part of real-world Java application development, debugging, and performance engineering.

7. The Bigger Picture

The JVM is one of the most important architectural decisions behind Java.

It separates the compilation of a program from the execution of that program on a particular machine.

The Java compiler produces platform-independent bytecode, while the JVM provides the runtime environment that makes this bytecode executable on supported platforms.

Its Class Loader Subsystem manages classes.

Its Runtime Data Areas organize execution memory.

Its Execution Engine interprets and compiles bytecode.

Its Garbage Collector manages memory reclamation.

Together, these components create a runtime environment that balances portability, performance, memory management, and security.

Understanding the JVM is therefore not just about learning another Java concept.

It is about understanding the machine that makes Java applications possible.


Conclusion

Java is often described as a high-level programming language, but its real strength comes from the ecosystem that executes it.

The JVM is not simply a bytecode interpreter. It is a sophisticated runtime system that dynamically loads classes, manages memory, executes instructions, optimizes performance, and coordinates with the underlying operating system.

As Java developers, understanding these mechanisms helps us move beyond writing code that works toward building applications that perform reliably and scale effectively.

The next step in our journey is to understand how Java manages memory in greater detail.

In the next article, we will explore:

Java Memory Management: Heap, Stack, Garbage Collection and Memory Leaks