Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

Describe the main components of the JVM and the runtime data areas it uses.

The JVM has three main parts: the class loading subsystem, which loads, links and initialises classes; the runtime data areas, where the program's data lives; and the execution engine, made of the interpreter, the JIT compilers and the garbage collector. The runtime data areas are the heap for objects, metaspace for class metadata and the code cache for compiled code, all shared by every thread, plus a Java stack, a program counter and a native method stack for each thread. Native interfaces such as JNI and the Foreign Function and Memory API connect Java to native code.

Why this question is asked

Most of the time the JVM is invisible: we write classes, run java -jar, and things work. But nearly every production problem with a Java service lands in one of its parts: OutOfMemoryError in the heap or in metaspace, StackOverflowError in a thread stack, slow startup in class loading, a warm-up period in the JIT, pauses in the garbage collector. Interviewers ask this question because knowing the map makes every error message easier to place. In this answer, we will walk through the JVM from a .class file to running code: how classes get in, where each kind of data lives, how bytecode is executed, and how Java talks to native code.

1The Big Picture

The JVM is a program that runs other programs written as bytecode, the instruction format in .class files. It takes those files, turns them into in-memory classes, gives the program memory to work with, executes the bytecode, cleans up unused objects, and connects to the operating system when the program needs native functionality. Each of those jobs is done by a different component.

Step throughMain parts of the JVM
  1. Class loading subsystemload, link and initialise classes from .class files and modules
  2. Runtime data areas, sharedheap, metaspace, code cache
  3. Runtime data areas, per threadJava stack, program counter, native method stack
  4. Execution engineinterpreter, JIT compilers C1 and C2, garbage collector
  5. Native interfacesJNI and the Foreign Function and Memory API
  6. Operating system and hardwarethreads, memory, files, sockets

6 layers, from the top down. Step through them one at a time.

2The Class Loading Subsystem

Before any code can run, its class must be loaded. Class loaders find the bytes of a class, from the JDK's modules, from a JAR on the class path or from somewhere else entirely, and define a class from them. Then linking verifies that the bytecode is safe and well formed, prepares static fields with default values and resolves symbolic references to other classes. Finally initialization runs the class's static initializers. The JVM does this lazily, class by class, the first time each class is needed, which is one reason large applications take time to start.

3The Shared Runtime Data Areas

The heap holds every object and array the program creates. It is shared by all threads, and it is the area the garbage collector manages; its maximum size is set with -Xmx or, in containers, as a percentage of the container memory. When live objects no longer fit, we get OutOfMemoryError: Java heap space.

Metaspace holds class metadata: each loaded class's structure, its methods and their bytecode, and its constant pool. It is native memory outside the heap, and it is freed only when a class loader and all its classes become unreachable. Before Java 8 this data lived in the permanent generation, part of the heap; metaspace replaced it.

The code cache holds machine code produced by the JIT compilers, along with some code the JVM generates for itself. It has a fixed maximum size, ReservedCodeCacheSize. All three are shared, which is why classes and compiled code are loaded and compiled once per JVM, not once per thread.

4The Per-Thread Runtime Data Areas

Every thread gets its own Java stack, made of frames. Each method call pushes a frame holding that call's local variables, its operand stack (bytecode is stack-based, so values are pushed and popped while computing), and a reference to the method's class. When the method returns, the frame is popped. Deep or infinite recursion fills the stack and throws StackOverflowError; the default stack size is often 1 MB and can be set with -Xss.

Each thread also has a program counter, holding the address of the bytecode instruction it is executing, and a native method stack for calls into native code. Because these areas belong to one thread, local variables are naturally thread-safe, while anything on the heap may be shared and needs care.

Java
void handle(Request req) { // frame for handle(): req, total
int total = price(req) * 2; // price() pushes a new frame on top, then pops it
Order o = new Order(total); // the Order object is on the heap; o, the reference, is in this frame
}
Where each kind of data lives
DataAreaShared or per thread
new Order(...) objectHeapShared
Order.class structure and bytecodeMetaspaceShared
JIT-compiled machine code of handle()Code cacheShared
Local variables total and oJava stack framePer thread
Current instruction addressProgram counterPer thread
Frames of a native library callNative method stackPer thread

5The Execution Engine

The execution engine runs the bytecode. At first, the interpreter executes it instruction by instruction, which starts quickly but runs slowly. While it does so, the JVM counts how often each method runs and which branches it takes. Hot methods are compiled to machine code by the JIT, the just-in-time compiler. HotSpot has two: C1 compiles quickly with light optimisation, and C2 compiles hot code later with aggressive optimisation, using the profile gathered so far to inline calls and remove checks. This tiered compilation is why a Java service gets faster during its first minutes, the warm-up.

The garbage collector is also part of the execution engine. It finds objects on the heap that are no longer reachable from any thread stack, static field or other root, and reclaims their memory. Java 25 offers several collectors, such as G1, the default for most machines, ZGC for very low pauses, Parallel for throughput, and Serial for small heaps.

6Native Interfaces

Some functionality lives outside Java: operating system calls, hardware libraries, or existing C code. JNI, the Java Native Interface, has connected Java to native code since the early days, but it requires writing C glue code and is easy to get wrong. The Foreign Function and Memory API, final since Java 22, lets Java code call native functions and work with off-heap memory directly from Java, with safer memory handling. Both run native frames on the thread's native method stack.

7Follow-up questions

Is the string pool in the heap or in metaspace?

In the heap. Since Java 7 interned strings are stored as normal heap objects, managed by the garbage collector. Metaspace holds class metadata, and the class's constant pool there refers to those strings rather than containing the string objects themselves.

Which areas can throw OutOfMemoryError, and how do the messages differ?

The heap gives Java heap space, metaspace gives Metaspace, and other native exhaustion gives messages such as Direct buffer memory for direct ByteBuffers or unable to create native thread when the OS refuses a new thread stack. A full code cache does not throw; the JVM logs a warning and stops compiling. The message tells us which area to investigate.

Are objects always allocated on the heap?

Conceptually yes. In practice, the C2 compiler's escape analysis can prove that an object never leaves a method and then replace it with plain local values, so no heap allocation happens at all. That is an optimisation invisible to the program; the language rule remains that objects live on the heap.

8Conclusion

In this answer, we followed a class through the JVM. The class loading subsystem loads, links and initialises it; its metadata goes into metaspace and its objects onto the shared heap; each thread runs it on its own stack of frames with a program counter and a native method stack; the execution engine interprets the bytecode, compiles hot methods with the C1 and C2 JIT compilers into the shared code cache, and reclaims unreachable objects with the garbage collector; and JNI and the Foreign Function and Memory API connect it to native code.

What to say in the interviewAbout 90 seconds aloud

Open with

I would describe the JVM in three main parts plus native interfaces. First, the class loading subsystem finds class bytes, defines the class, links it, which means verifying the bytecode, preparing static fields and resolving references, and then initialises it by running static initializers, lazily on first use.

Then, if they want more

Second, the runtime data areas. Shared by all threads are the heap, where every object and array lives and which the garbage collector manages; metaspace, native memory holding class metadata such as the class structure, methods and bytecode, which replaced the permanent generation in Java 8; and the code cache, holding JIT-compiled machine code.

Per thread, there is a Java stack of frames with local variables and an operand stack, a program counter for the current instruction, and a native method stack. That split explains why locals are thread-safe and heap objects need care.

Third, the execution engine: the interpreter starts running bytecode immediately, the JVM profiles hot methods, and the C1 and C2 JIT compilers turn them into optimised machine code, which is why Java warms up. The garbage collector, G1 by default, reclaims unreachable objects.

Finally, JNI and the newer Foreign Function and Memory API, final in Java 22, let Java call native code. Each common error maps to one of these: heap space, Metaspace, StackOverflowError, or a full code cache.

About this answer

Last updated
12 November 2026
Versions it names
Java 22, Java 25, Java 7, Java 8
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Describe the main components of the JVM and the runtime data areas it uses.": https://scrivio-interview-library.netlify.app/java/jvm-architecture/23df704cc7/