Part 13 / Topic 1 of 8
Stack vs. heap
Every thread gets its own stack: a region of memory holding one frame per method call currently in progress — its parameters, local variables, and where to return to when it's done. Call a method, push a frame; return from it, pop the frame. There is exactly one stack per thread, and it's strictly first-in-last-out.
The heap is one shared region (one per JVM, not per thread) where every object lives — anything created with new, every array, every String not already in the pool (section 3). Objects on the heap don't disappear when a method returns; they stay until nothing refers to them any more and the garbage collector reclaims them (section 2).
Think of the stack as a stack of trays at a canteen counter — the last tray put down is the first one picked up — and the heap as the kitchen's shared shelf of ingredients, which anyone with a reference to a shelf slot can still reach long after whoever put it there has gone home.
Runtime runtime = Runtime.getRuntime();
System.out.printf("max heap: %,d bytes%n", runtime.maxMemory());
$ javac StackVsHeap.java
$ java StackVsHeap
== 1. Heap: objects live here, and the JVM reports its size ==
max heap: 12,884,901,888 bytes (12288 MiB)
total heap: 813,694,976 bytes (776 MiB) -- currently handed to the JVM by the OS
free heap: 809,929,208 bytes (772 MiB) -- unused inside that total
== 2. Allocating 2,000,000 small objects moves free heap down ==
free heap before: 809,258,008 bytes
free heap after: 727,665,176 bytes
heap used by 2,000,000 int[1] objects: ~81,592,832 bytes (40.8 bytes each, object header + the ArrayList's own backing array)
(varies by machine and run — whether the JVM had already grown the heap changes this number)
== 3. Stack: recursion without a base case runs out of frames, not heap ==
StackOverflowError caught -- the JVM ran out of stack space, not heap space.
The heap numbers above were untouched by this; it is a completely separate memory area.
maxMemory() is the hard ceiling (-Xmx, or a JVM-chosen default — a fraction of physical RAM if you never set one); totalMemory() is what the JVM has actually asked the OS for so far, which grows as needed up to the max; freeMemory() is unused space inside that current total. Allocating 2,000,000 tiny int[1] arrays visibly moves freeMemory() down — each one of those is a real heap object with its own header, not just "4 bytes for one int". Recursing with no base case throws StackOverflowError, not an out-of-memory error — proof that the stack and the heap are separate pools that fail independently of each other.
| Stack | Heap | |
|---|---|---|
| One per | thread | JVM (shared by all threads) |
| Holds | method frames: parameters, local variables, return address | objects: anything made with new, arrays, non-pooled Strings |
| Lifetime | the frame's method call | until unreachable and collected |
| Runs out via | deep/infinite recursion → StackOverflowError |
too many live objects → OutOfMemoryError |
Full program: examples/StackVsHeap.java.
Done here? The practice set for Part 13 — including the solutions — is on the part page.