Sub topic 1
Where the JVM Keeps Your Data
Prerequisites
You should know basic Java syntax, classes, objects, and references. You should also know how to compile and run a Java program from the command line. This chapter uses Java 11 terminology and HotSpot behavior. Some implementation details can differ between JVMs.
A Java program uses several runtime memory areas. The JVM creates some areas for the whole process and others for each thread. The heap stores objects, while a thread's stack stores method-call data. Class metadata lives in Metaspace. These areas have different purposes and failure modes.
The main memory areas
| Area | Shared or private | Typical contents |
|---|---|---|
| Heap | Shared by Java threads | Objects, arrays, and most class instances |
| Java stack | Private to each thread | Stack frames, local variables, and method state |
| Metaspace | Shared | Class structure and runtime class metadata |
| Program counter | Private to each thread | The current bytecode instruction |
| Native method stack | Usually private | State for native, non-Java code |
The heap is the main area managed by garbage collection. When you write new Customer(), the Customer object normally goes on the heap. Arrays also normally go on the heap. A reference variable does not contain the object itself. It contains a way to locate the object.
A heap can be divided into regions or generations by the garbage collector. Those details affect collection behavior, but the basic rule remains the same: heap objects can stay alive after the method that created them returns.
The Java stack belongs to one thread. Each method call creates a stack frame. A frame holds the method's parameters, local variables, intermediate values, and a return location. When the method returns, its frame is removed.
Consider this example:
public class MemoryAreas {
static int sharedCount = 10;
public static void main(String[] args) {
Person person = new Person("Mina");
printName(person);
}
static void printName(Person value) {
String label = value.name();
System.out.println(label);
}
record Person(String name) {}
}When main runs, its frame contains the local reference person. The Person object is on the heap. Calling printName creates another frame containing value and label. Both person and value refer to the same heap object. Returning from printName removes its frame, but the object remains until no live reference can reach it.
The static field sharedCount belongs to the class rather than to one object. The JVM stores the field's value as part of the class's runtime data. The exact physical placement is JVM-specific, so reason about its lifetime and sharing rather than assuming a fixed address.
Metaspace and other native memory
Metaspace stores metadata for loaded classes, such as method descriptions, field information, and inheritance relationships. It uses native memory rather than the Java heap. Loading many classes, or repeatedly creating class loaders that cannot be unloaded, can therefore exhaust native memory even when the heap is not full.
The JVM also uses native memory for thread stacks, compiled code, garbage-collection data, direct byte buffers, and internal structures. A container or operating system can terminate a process when its total memory exceeds a limit, even if the Java heap still has free space.
A useful mental model
Think of a reference as an arrow from a stack frame, static field, or another heap object to an object on the heap. Garbage collection follows these arrows from special starting points called GC roots. Reachability, rather than the scope of a variable alone, determines whether an object can be reclaimed.
The next chapter follows those arrows and explains when an object becomes garbage.
Exercise
In the example, what happens to the printName stack frame when the method returns, and what happens to the Person object?
Show answer
The printName frame is removed. The Person object stays on the heap while a live reference can still reach it. After all such references disappear, it may become eligible for garbage collection.