C

Core Java tutorial for beginners

Clean • Professional

Class Loading Process & JIT Compiler in Java – How JVM Loads & Executes Code

2 minute

Class Loading Process & JIT Compiler in Java

The Class Loading Process and the JIT Compiler are two of the most important parts of the JVM Execution Engine.

They ensure that Java programs run securely, efficiently, and with high performance.


Class Loading Process in Java

Java follows a dynamic class loading mechanism—classes are loaded into memory only when needed.

The Class Loading Process involves three major phases:

learn code with durgesh images

Phase 1: Loading

During this step, the JVM:

  • Reads the .class (bytecode) file
  • Allocates memory for class structure
  • Creates Class objects in the Method Area
  • Uses Class Loaders to load classes dynamically

Class Loaders Involved

  1. Bootstrap Class Loader (Loads core Java classes)
  2. Extension/Platform Class Loader (Loads JDK extensions)
  3. Application Class Loader (Loads project-level classes)

Phase 2: Linking

Linking checks the correctness of the bytecode and prepares it for execution.

It has three sub-steps:

1. Verification

Ensures the bytecode is safe, valid, and follows JVM rules.

  • Checks stack size
  • Checks variable types
  • Prevents malicious bytecode
  • Ensures no illegal memory access

2. Preparation

Allocates memory for static variables of the class.

Default values are assigned (e.g., 0, null, false).

3. Resolution

Converts symbolic references into direct references.

Example: method names → actual memory addresses.


Phase 3: Initialization

This phase runs:

  • Static blocks
  • Static variable initializers

Example:

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

Initialization happens only once in program lifetime.


Role of the JIT Compiler

The JIT (Just-In-Time) Compiler is part of the JVM Execution Engine.

Its job is to improve runtime performance by compiling frequently executed code (hotspots) into native machine code.


How JIT Works

  1. Interpreter starts executing bytecode
  2. JVM identifies frequently used code (loops, methods)
  3. JIT compiles that code to native CPU instructions
  4. Future executions run the optimized machine code
  5. Application becomes significantly faster

Types of JIT Optimizations

JIT performs multiple high-level optimizations:

learn code with durgesh images

1. Method Inlining

Replaces method calls with the actual code block.

2. Dead Code Elimination

Removes unused/unreachable code.

3. Loop Unrolling

Expands loops to reduce overhead.

4. Escape Analysis

Determines if an object can be safely allocated on the stack instead of heap.

5. Constant Folding

Replaces constant expressions with precomputed values.


HotSpot JVM (Most Common in Java)

The HotSpot JVM includes:

  • Interpreter
  • Client Compiler (C1) → quick optimization
  • Server Compiler (C2) → advanced optimization

This combination gives:

  • Fast startup time
  • High long-term performance

Class Loader + JIT: How They Work Together (Flow)

Source Code (.java)
       |
       v
   javac Compiler
       |
       v
 Bytecode (.class)
       |
       v
+---------------------+
|  Class Loader        |
| Loading/Linking/Init |
+---------------------+
       |
       v
+-------------------------+
|   Interpreter            |
+-------------------------+
       |
       v
[HotSpot Profiler]
       |
       v
+-------------------------+
|       JIT Compiler      |
| (Generates Machine Code)|
+-------------------------+
       |
       v
     CPU Execution

Why This Matters

  • Understanding Class Loading + JIT helps you:
  • Improve application performance
  • Reduce startup delays
  • Optimize heavy loops & critical code paths
  • Diagnose class loading issues
  • Handle ClassNotFound & NoClassDefFoundError
  • Write efficient multi-module applications

Article 0 of 0