Sub topic 4
Where Data Lives: Heap, Metaspace, and Thread Stacks
Assume you can write and run basic Java programs and understand classes, methods, static members, and exceptions. You should also know how to use javac and java.
When the JVM runs a Java program, it divides runtime data into areas with different purposes and lifetimes. The most important areas are the heap, thread stacks, and metaspace. The JVM specification defines some areas abstractly. HotSpot, the common Java 11 JVM, provides the concrete implementation described here.
The heap: objects and arrays
The heap is shared by all threads. It stores objects and arrays created with new. The garbage collector finds objects that can no longer be reached and reclaims their memory.
A local variable usually stores a reference, not the object itself:
public class MemoryDemo {
static int total;
public static void main(String[] args) {
int count = 3;
int[] values = new int[count];
values[0] = 10;
values[1] = 20;
values[2] = 30;
total = sum(values);
System.out.println(total);
}
static int sum(int[] numbers) {
int result = 0;
for (int number : numbers) {
result += number;
}
return result;
}
}The values variable is a local variable in main's stack frame. It refers to an array object on the heap. The numbers parameter in sum refers to the same array. No second array is created when the method is called.
The heap is logically shared, but its layout is an implementation detail. HotSpot commonly divides it into young and old generations to make garbage collection efficient. Do not rely on a particular object address or generation.
Thread stacks and frames
Each Java thread has its own JVM stack. A stack contains frames, and the JVM creates one frame for each active method invocation. A frame contains:
- Local variables, including primitive values and object references
- An operand stack used by bytecode instructions
- A reference to the runtime constant pool for the current class
- Information needed to return from the method
In the example, main has a frame containing count, values, and total. While sum runs, a second frame contains numbers, result, and the loop variable. The sum frame is removed when the method returns.
Because each thread has a separate stack, local variables are normally thread-confined. The array referenced by values, however, is on the shared heap. If multiple threads share that reference, they can access the same object and may need synchronization.
A deeply recursive method can exhaust its thread stack and throw StackOverflowError. A large number of heap objects can instead cause an OutOfMemoryError from the heap.
Metaspace and other runtime areas
Metaspace is HotSpot's native-memory implementation for class metadata. It holds information such as class structure, method metadata, and runtime representations used by the JVM. It is shared between threads.
Metaspace is not the same as the heap. Garbage collection can unload class metadata when its class loader and loaded classes are no longer reachable. Applications that repeatedly create class loaders, such as plugin systems, can therefore exhaust metaspace if old loaders remain reachable.
Other JVM runtime areas include:
- A program counter for each thread, identifying the next bytecode instruction for that thread
- A native method stack for native code used through mechanisms such as JNI
- A runtime constant pool containing symbolic information associated with each loaded class
A practical diagnosis
The area that fails often suggests the problem:
| Failure or symptom | Likely area | Typical cause |
|---|---|---|
| `StackOverflowError` | Thread stack | Excessive recursion or very deep calls |
| Heap `OutOfMemoryError` | Heap | Too many reachable objects |
| Metaspace `OutOfMemoryError` | Metaspace | Too many retained class loaders or generated classes |
These categories are clues, not absolute proof. Use JVM logs, heap dumps, and profilers to confirm the cause.
Exercise
In the MemoryDemo program, which data is private to the current method call, and which data is shared through the heap?
Show answer
count, values, total, numbers, result, and the loop variable are stored in method frames or represented there as locals. The integer array is a heap object. Both values and numbers refer to that shared array while sum runs.