Back to Study Material Java

JVM Architecture

The Java Virtual Machine is the runtime environment that executes Java bytecode. Its architecture is what enables Java's platform independence: you compile...

JVM Architecture

Inside the Java Virtual Machine

The Java Virtual Machine is the runtime environment that executes Java bytecode. Its architecture is what enables Java's platform independence: you compile Java source code to bytecode once, and the JVM translates it to native machine code on whatever operating system it runs on. Understanding the JVM's internal components helps you diagnose performance problems, memory issues, and unexpected runtime behavior.

The JVM specification defines an abstract machine. Concrete implementations, called JVM distributions, include HotSpot (the reference implementation from Oracle), OpenJ9 (from Eclipse), and GraalVM. These implementations follow the specification but differ in optimization strategies and additional features.

The class loader subsystem is responsible for loading class files into the JVM. It follows a delegation model with three built-in loaders. The bootstrap loader loads core Java classes from the runtime jar. The extension loader handles classes in the ext directory. The application loader loads classes from the application classpath. Custom class loaders can load classes from databases, networks, or encrypted archives.

The runtime data areas are the memory regions used during execution. The method area stores class metadata and bytecode. The heap stores all objects. Each thread has its own stack, containing frames for active method calls. The PC register tracks the current bytecode instruction for each thread. Native method stacks support JNI calls to native code.

The execution engine is where bytecode is actually run. The interpreter executes bytecode instructions one at a time. The Just-In-Time compiler identifies hot methods (methods called frequently) and compiles them to native machine code for much faster execution. HotSpot uses a tiered compilation strategy with the client compiler (C1) for quick compilation and the server compiler (C2) for aggressive optimization.

Example showing JVM info at runtime:

System.out.println(System.getProperty("java.vm.name")); System.out.println(System.getProperty("java.version"));

Interview tip: Explain what happens when a class is loaded for the first time. The JVM loads the bytecode, verifies it for correctness, prepares static fields with default values, resolves symbolic references, and runs the static initializer block. This sequence is called class initialization.

JIT Compilation and Performance

The Just-In-Time compiler is the component of the JVM most responsible for Java's runtime performance. When the JVM starts, it interprets bytecode directly. Interpretation is flexible but slow compared to native execution. The JIT monitors which methods are executed frequently and compiles those hot methods to native machine code, eliminating interpretation overhead for the most critical parts of the program.

HotSpot uses a tiered compilation system with four tiers. Tier 0 is pure interpretation. Tier 1 uses the C1 compiler to produce lightly optimized native code quickly. Tier 2 also uses C1 but collects profiling data. Tiers 3 and 4 use the C2 server compiler, which applies aggressive optimizations based on the profiling data collected earlier. A method typically moves from interpretation to tier 1 or 2 quickly, then is recompiled by C2 once enough profiling data is available.

C2 applies optimizations that go beyond what a static compiler can do because it can observe actual runtime behavior. If a virtual method call always dispatches to the same implementation, C2 can inline the target directly, eliminating the dispatch overhead. If an if-statement is always true, C2 may remove the false branch entirely. These speculative optimizations are backed by runtime guards; if the assumption is violated, the JVM deoptimizes the compiled code and reverts to interpretation.

Method inlining is one of the most impactful JIT optimizations. When a small method is called from a hot loop, the JIT replaces the call with the method body, eliminating call overhead and enabling further optimizations like constant folding and dead code elimination. Methods are considered for inlining when they are small (below a configurable byte code size threshold) and called frequently.

You can observe JIT compilation with:

java -XX:+PrintCompilation -jar myapp.jar

This prints a line for each method compiled, showing the compilation tier and whether the method was subsequently deoptimized.

Loop unrolling, scalar replacement, and escape analysis are additional JIT techniques. Escape analysis determines whether an object is used only within a single method; if so, the JIT may allocate it on the stack rather than the heap, eliminating GC pressure for short-lived objects.

Interview tip: Explain what deoptimization means in the context of the JIT. When a speculative optimization turns out to be incorrect at runtime, the JVM must abandon the native code and return to a safe state. This is called deoptimization or uncommon trap. Frequent deoptimization, visible in JIT logs, indicates that profiling assumptions are being invalidated and can hurt performance.

Class Loading and the Classpath

Class loading in Java is the process by which the JVM finds, reads, and initializes class files at runtime. It is a dynamic process: classes are loaded on demand when they are first referenced, not all at once at startup. This lazy loading speeds startup time and reduces memory usage for large applications that do not use all their classes in every run.

The parent delegation model is central to class loading. When a class loader is asked to load a class, it first delegates to its parent. Only if the parent cannot find the class does the child attempt to load it. This ensures that core Java classes like java.lang.String are always loaded by the bootstrap loader and cannot be replaced by a rogue class on the application classpath.

Class loaders are themselves Java objects, instances of ClassLoader or one of its subclasses. You can write custom class loaders to load classes from non-standard sources. Application servers like Tomcat use multiple class loaders to isolate deployed applications from each other, so that a class in one web application cannot accidentally interfere with the same class name in another application.

The classpath is the list of directories and jar files the application class loader searches when looking for a class. It is specified with the -cp flag at the JVM command line or through the CLASSPATH environment variable. Since Java 9, modules provide an alternative to the classpath with stronger encapsulation and explicit dependency declarations.

When two classes with the same name exist on the classpath, the one found first wins. This class collision is a common source of bugs in applications with many dependencies and is one of the problems that modules were designed to solve.

Example of loading a class dynamically:

Class<?> clazz = Class.forName("com.example.MyService"); Object instance = clazz.getDeclaredConstructor().newInstance();

Class.forName() uses the calling class's class loader by default. You can pass a class loader explicitly to control which loader is used, which is important in environments with multiple class loaders.

Interview tip: Describe how class loading isolation works in an application server. Each deployed application gets its own class loader instance. When the application looks up a class, its loader searches the application's own classes first, then delegates upward. This prevents applications from seeing each other's classes while still sharing the server's infrastructure classes.