The Plain Picture#
The stage (shared by everyone): all the props and sets in use — the heap.
The script library backstage (shared): the scripts describing every scene — metaspace.
Each actor's personal notebook (private): what they're doing right now and where they are in the script — stack and PC register.
The Real Names#
| Area | Shared? | Holds | Error when full |
|---|---|---|---|
| Heap | all threads | every object and array (and their instance fields) | OutOfMemoryError: Java heap space |
| Metaspace | all threads | class metadata: class structures, method bytecode, the runtime constant pool. (Static fields are not here: since Java 8 they live on the heap, inside the class's java.lang.Class object) | OutOfMemoryError: Metaspace |
| JVM stack | one per thread | stack frames: local variables (primitives and references), operand stack, return address | StackOverflowError |
| PC register | one per thread | the address of the bytecode instruction being executed (the JVM's version of the CPU's program counter — How Computers Work Ch 5) | — |
| Native method stack | one per thread | frames for native (C/C++) methods | StackOverflowError |
| Code cache | all threads | machine code produced by the JIT (Ch 4) | JIT stops compiling ("CodeCache is full") |
Metaspace vs PermGen: before Java 8, class metadata lived in a fixed-size heap area called PermGen, famous for OutOfMemoryError: PermGen space.
Java 8 replaced it with metaspace, which lives in native memory and grows as needed (cap it with -XX:MaxMetaspaceSize). Metaspace leaks happen when
class loaders are created over and over and never freed — e.g. redeploying an app many times inside one server, or generating classes dynamically.
Under the Hood#
Stack frames and StackOverflowError, measured#
Each thread's stack has a fixed maximum size, set with -Xss. Infinite recursion until it overflows:
StackOverflowError at depth 47324 (default stack size)
StackOverflowError at depth 1466 (-Xss256k)
StackOverflowError at depth 98903 (-Xss4m)The depth scales with the stack size. Each frame here was small; frames with many local variables are bigger, so real recursions overflow sooner. This is why
a recursive DFS over 100,000 nodes can crash (Operating Systems Ch 6) — and why converting it to a loop with an explicit ArrayDeque stack
(Collections, From the Inside Ch 8) or running it on a thread created with a larger stack size fixes it.
OutOfMemoryError, measured — and a surprise#
Keep allocating 1 MB arrays with a 64 MB heap:
OutOfMemoryError: Java heap space after 30 MBOnly about 30 MB of our data fitted into a 64 MB heap. Two reasons, both worth knowing: the heap is also used by the JVM's own objects and the collector needs free space to work; and in the G1 collector (Ch 8), an object at least half a region in size is a "humongous" object that gets whole regions to itself — with a 64 MB heap, regions are 1 MB, and a 1 MB array plus its header needs two. Big arrays can waste heap in G1.
What an object costs — object layout#
Every object on the heap has a header before its fields: a mark word (8 bytes: hash code, lock state, GC age) and a class pointer (4 bytes with compression). Objects are padded to a multiple of 8 bytes. So:
new Integer(5): 12 bytes header + 4 bytes int = 16 bytes (vs 4 bytes for a plain int)
new int[0]: 12 bytes header + 4 bytes length = 16 bytes (an empty array still costs 16)
a record Cell(int r, int c): 12 + 4 + 4 = 20 → padded to 24 bytesThat's where the 3 MB vs 21 MB in Classes, Objects and Memory Ch 8 came from. Compressed oops ("ordinary object pointers") store references as 32-bit values instead of 64-bit, halving their size; it's on by default for heaps under ~32 GB — which is one reason people avoid heaps just over 32 GB.
Check yourself
5 questionsWhere do objects live? Local variables? Static fields? Method bytecode?
Show answerHide answer
Heap; the thread's stack frames (references there point to heap objects); on the heap, inside the class's java.lang.Class object; metaspace.
What replaced PermGen, and where does it live?
Show answerHide answer
Metaspace, in native memory (outside the Java heap), growing as needed.
Why did recursion reach depth 98,903 with -Xss4m but only 1,466 with -Xss256k?
Show answerHide answer
-Xss sets each thread's stack size; a bigger stack holds more frames.
Why did only ~30 MB of 1 MB arrays fit into a 64 MB G1 heap?
Show answerHide answer
JVM overhead and GC headroom, plus each 1 MB array was a humongous object occupying two 1 MB regions.
How big is an Integer object, and why?
Show answerHide answer
16 bytes: a 12-byte header (mark word + compressed class pointer) plus the 4-byte int value, padded to a multiple of 8.