JVM Internals & ClassLoader

Clean HTML study notes — Virtual Machines, JDK/JRE/JVM, JVM Architecture, Runtime Data Areas and Class Loading

Source: cj7-virtualmachine-classloader.pdf
Converted into the previous clean HTML study-notes format. Original PDF Pages are intentionally not included.

1. Virtual Machine

A Virtual Machine (VM) is software that emulates a computer system and provides an environment for running applications without requiring the application to interact directly with the physical hardware.

A VM can provide an isolated environment in which applications or operating systems can execute while sharing the underlying host resources.

2. Types of Virtual Machines

TypeMeaningMain purpose
Hardware/System Based VMProvides multiple logical computer systems on one physical host.Run multiple operating-system environments while sharing hardware resources.
Software/Process/Application Based VMActs as a runtime engine for a particular application or programming language.Provide a platform-independent execution environment for a process.

3. Hardware Based / System Based Virtual Machine

System VMs provide isolated logical systems on the same physical computer. The host's hardware resources can be shared and managed among multiple virtual machines.

Examples

  • KVM (Kernel-based Virtual Machine) for Linux
  • VMware
  • Xen
  • Cloud computing environments

Advantages

  • Effective utilization of hardware resources.
  • Multiple operating systems can coexist on one physical host.
  • Host and guest environments can share selected resources and files.
  • Developers can work in isolated environments without changing the host operating system.
  • Virtualization software can support provisioning, high availability, maintenance and disaster recovery.

4. Software Based / Process Based Virtual Machine

Process-based VMs act as runtime engines for particular programming-language applications. They are also called application VMs or Managed Runtime Environments (MREs).

  • They run as normal applications inside the host operating system.
  • They generally support a single process.
  • They are started when the process starts and destroyed when the process exits.
  • They provide a platform-independent execution environment.

Examples

VMRuntime purpose
JVMRuns Java applications.
PVM (Parrot VM)Runs Perl-related programs/scripts.
CLRRuntime engine for .NET applications.

5. Virtual Machine Language / Bytecode

A virtual-machine-oriented language representation can be executed through a VM rather than being tied directly to one operating system's native instruction set.

For Java, source code is compiled into bytecode. The JVM interprets or JIT-compiles that bytecode for the underlying platform.

Java Source CodeJava Compiler.class BytecodeJVMNative Execution

This is the foundation of Java's write once, run anywhere model: the same bytecode can be executed on platforms for which a compatible JVM exists.

6. JDK, JRE and JVM

ComponentPurpose
JDK — Java Development KitUsed to develop and run Java applications. Contains development tools and a runtime environment.
JRE — Java Runtime EnvironmentProvides the runtime environment and libraries required to run Java applications.
JVM — Java Virtual MachineExecutes Java bytecode.
JDK = JRE + Development Tools
JRE = JVM + Java Libraries

Conceptually, the JRE is the runtime portion of the JDK, and the JVM is the execution engine inside that runtime environment.

Simple flow

DeveloperJDKJREJVMJava Application

7. JVM Internal Architecture

The JVM is responsible for loading and executing Java applications. Its major runtime components include the Class Loader Subsystem, Runtime Data Areas, Execution Engine, JNI and Native Method Libraries.

Class Files
Class Loader Subsystem
Method Area
Heap
Stack Area
PC Registers
Native Method Stacks
Execution Engine
Java Native Interface (JNI)
Native Method Libraries

The architecture diagram in the source PDF shows class files entering the Class Loader Subsystem, runtime data areas supplying execution state, and the Execution Engine interacting with JNI and native libraries. fileciteturn18file0L13-L17

8. Runtime Data Areas

AreaPurposeScope
Method AreaStores class-level information and runtime structures associated with loaded classes.Per JVM.
HeapStores objects and arrays created by Java programs.Per JVM.
Java StackStores method invocation frames, local variables, return information and intermediate calculations.Per thread.
PC RegisterIndicates the next instruction to execute for the current thread.Per thread.
Native Method StackSupports invocation/execution state for native methods.Per thread.

9. Stack, Stack Frames and PC Register

Whenever a new thread is created, the JVM provides that thread with its own PC register and JVM stack.

  • The PC register identifies the next instruction to execute for the current thread.
  • The stack stores method invocations.
  • Each method invocation creates a stack frame.
  • A frame contains information such as local variables, return information and intermediate calculations.
  • When a method completes, its frame is automatically removed from the stack.
Thread → PC Register + Java Stack
Java Stack → Stack Frame 1 + Stack Frame 2 + Stack Frame 3 ...

The source notes explicitly describe PC registers and stacks as thread-specific and explain that every method entry is represented by a stack frame. fileciteturn18file0L21-L35

10. Class Loader Subsystem

The Class Loader Subsystem is responsible for bringing class information into the JVM and making it ready for execution.

Three major activities

  1. Loading
  2. Linking
  3. Initialization
Class FileLoadingLinkingInitializationReady for execution

These three responsibilities are listed in the source notes. fileciteturn17file0L119-L123

11. Loading

Loading means reading a class file and storing the corresponding binary information in the Method Area.

For each loaded class, the JVM maintains the class-level metadata needed to represent and use that class. After loading, the JVM creates a corresponding java.lang.Class object on the Heap to represent the class-level binary information.

.class FileClass LoaderMethod Area+Class Object on Heap

12. Linking

Linking consists of three activities:

  1. Verification
  2. Preparation
  3. Resolution

13. Verification

Verification checks whether the binary representation of a class is structurally correct and whether the class file was generated by a valid compiler according to JVM rules.

  • The bytecode verifier performs this validation.
  • If verification fails, the JVM can throw java.lang.VerifyError.
Remember: Verification is a safety and correctness check performed before the class is allowed to proceed through the rest of linking.

14. Preparation

During Preparation, the JVM allocates memory for class-level static variables and assigns their default values.

The original explicitly assigned values are applied later during the Initialization phase.

PhaseStatic field state
PreparationMemory allocated and default values assigned.
InitializationExplicit/original initialization values are assigned and initialization logic runs.

15. Resolution

Resolution converts symbolic references used by the class into direct references to the actual runtime entities.

For example, class names stored in the constant pool can be resolved by locating the corresponding loaded class information in the Method Area.

Symbolic ReferenceSearch loaded class informationDirect Reference

The source example explains that classes such as Test, String, Student and Object may be loaded, while their names in the constant pool are resolved to actual references during this phase. fileciteturn17file0L158-L167

16. Initialization

Initialization is the phase in which the class is initialized, including assigning explicitly specified static field values and executing applicable class initialization logic.

In simple terms:

LoadingLinkingInitialization

During preparation static fields receive default values; during initialization their explicit initialization values are established.

17. Errors During Loading, Linking and Initialization

If an error occurs during the class loading/linking/initialization process, the JVM can report a java.lang.LinkageError or one of its subclasses. The source specifically notes that VerifyError is a child of LinkageError. fileciteturn17file0L172-L173

ProblemPossible error
Invalid bytecode/class structure during verificationVerifyError
Other class loading/linkage incompatibilitiesLinkageError or subclass

18. Java ClassLoader

ClassLoader is an abstract class in the java.lang package. It is responsible for finding and loading classes at runtime from available resources.

Key idea

When the JVM needs a class, the class-loading mechanism checks whether the class is already loaded. If not, a ClassLoader hierarchy is used to locate and load it.

19. ClassLoader Principles

Java ClassLoader behavior is commonly explained using three principles:

1. Delegation Principle

A child ClassLoader delegates a class-loading request to its parent first. The child attempts to load the class only if the parent cannot find it.

2. Visibility Principle

A child ClassLoader can see classes loaded by its parent. A parent generally cannot see classes that exist only in a child ClassLoader.

3. Uniqueness Principle

The delegation model helps ensure that a class is not unnecessarily loaded multiple times by different levels of the hierarchy.

20. ClassLoader Delegation Hierarchy

Bootstrap ClassLoader
Platform / Extension ClassLoader
Application ClassLoader
Application / User Code

The source notes describe the request flowing from the Application ClassLoader toward the Extension ClassLoader and then the Bootstrap ClassLoader, with the parent searched before the child performs its own loading. fileciteturn17file0L197-L214

Modern Java note: Since Java 9, the traditional “Extension ClassLoader” terminology is replaced by the Platform ClassLoader, and the old jre/lib/ext extension mechanism no longer applies. The delegation concept remains important.

21. Class Loading Flow

  1. JVM encounters a class reference.
  2. It checks whether the class is already loaded.
  3. If already loaded, the existing loaded class is reused.
  4. If not loaded, the Class Loader Subsystem receives the request.
  5. The Application ClassLoader delegates to its parent.
  6. The parent hierarchy is searched according to delegation.
  7. If no parent can load the class, an appropriate child loader attempts to find it.
  8. If the class cannot be found in the application class path, ClassNotFoundException can occur for an application-level lookup.
JVM requests classAlready loaded?Delegate to parentFind classLoad & use

22. JIT Compiler

JIT (Just-In-Time) Compiler is part of the JVM's execution machinery and is used to improve runtime performance.

Instead of interpreting every bytecode instruction repeatedly, frequently executed code can be compiled into native machine code and reused, reducing execution overhead.

BytecodeExecution EngineFrequently used codeJIT compilationNative machine code

23. JNI and Native Method Libraries

JNI (Java Native Interface) provides a bridge between Java code and native code/libraries.

  • Native methods are implemented outside ordinary Java bytecode.
  • JNI allows the JVM to interact with native methods.
  • Native method libraries provide the native implementation used by those methods.
Java CodeJNINative Method Libraries

24. Class File Structure — Magic Number

A Java .class file starts with a well-known magic number: 0xCAFEBABE.

The JVM uses this value to recognize a Java class file and validate that the file has the expected class-file format.

Magic Number:
0xCA FE BA BE

25. Class File Version — Minor and Major Version

Java class files contain minor and major version information. These values identify the class-file format/compiler version expected by the JVM.

This is one reason a class compiled for a newer Java release may not run on an older JVM: the older JVM may reject the unsupported class-file version.

26. Per-JVM vs Per-Thread Areas

Per JVMPer Thread
Method AreaJava Stack
HeapPC Register
Native Method Stack

The source notes summarize Method Area and Heap as belonging to the JVM, while Stack Area, PC Registers and Native Method Stack are associated with individual threads. fileciteturn17file0L219-L229

27. Complete JVM Execution Flow

Java Source Code
Java Compiler
.class Bytecode → Class Loader Subsystem
Loading
Linking
Initialization
Method Area
Heap
Stacks
PC Registers
Native Stacks
Execution Engine + JIT
JNI + Native Method Libraries

28. Interview / Quick Revision

  • What is a VM? Software that emulates/provides a managed environment for executing applications.
  • What is JVM? The Java Virtual Machine that loads and executes Java bytecode.
  • JDK vs JRE vs JVM? JDK is for development; JRE provides runtime libraries/environment; JVM executes bytecode.
  • What are ClassLoader phases? Loading, Linking and Initialization.
  • What are linking activities? Verification, Preparation and Resolution.
  • What happens in Preparation? Memory is allocated for static fields and default values are assigned.
  • What happens in Resolution? Symbolic references are resolved to direct references.
  • What does a PC register store? The next instruction to execute for a thread.
  • What does a stack frame contain? Method invocation information such as local variables, return information and intermediate calculations.
  • What is the ClassLoader delegation principle? A child asks its parent to load a class before trying itself.
  • What is the class-file magic number? 0xCAFEBABE.
  • What is JIT? A runtime compiler used to improve execution performance.
  • What is JNI? A bridge between Java and native code/libraries.