← Java Minute

What is the stack of a thread?

Published on 2026-08-19·v1.0

A portion of memory. Fortunately, this is something you do not need to take care of in Java — it is managed for you by the JVM.

Question

What is the stack of a thread?

What It Is

The stack of a thread is the portion of memory used by that thread to store everything it needs while it executes. It holds all the local variables created by the code being run, and it can hold references to objects on the heap.

The references themselves live on the stack; the memory they point to lives on the heap.

Stack vs. Heap

What lives on the stack is not shared: each thread has its own stack. That is not the case for the heap, where all the threads of your application can read and write data.

Because the heap is shared, it is where race conditions can happen. There is no race condition for what lives on the stack, since no other thread can see or touch it.

Stack Size

Threads are a system resource, and the size of a thread's stack is fixed at the system level. It may vary from one operating system to another, but it is typically several megabytes of memory.

Practical Example

This is also why deep, unbounded recursion eventually throws a StackOverflowError: each recursive call pushes a new frame with its own local variables onto the thread's stack, and once that fixed-size stack is exhausted, there is no more room left.

Watch it happen: frames pushed and popped by fib(3)

A naive recursive fib(n) calls itself twice per level. Each call pushes a new frame onto the thread's stack; each return pops it back off. Watch the depth grow to 3 and shrink back to 0 as fib(3) unwinds:

Only 4 levels deep here, so it fits comfortably. Remove the base case, or recurse thousands of levels deep, and this same growth pattern runs straight into StackOverflowError — the thread's fixed-size stack has no more room for another frame.

References