Skip to main content
PreparationZone Logo
PrepZone

Stack and Heap

Which memory region holds your variable, which holds the object, and why that distinction matters.

Pass checkpoint below

Why this matters

  • StackOverflowError and OutOfMemoryError are both memory failures, and the one you get tells you exactly which region filled up.
  • Thread safety only exists as a concern because stacks are private and the heap is shared.
  • Every later topic — garbage collection, memory leaks, escape analysis — assumes you can place a given value in the right region.

The two regions

Stack — per thread
main() frameuser → reference
greet() framecount = 3 (primitive)

Popped the moment a method returns.

Heap — shared
User objectname = "Asha"
String object"Asha"

Cleared by the garbage collector, not by returning.

The variable sits in the thread's stack frame. The object it names lives in the shared heap, which is why two variables can point at one object.
AspectStackHeap
HoldsMethod frames: locals, parameters, references, return addressEvery object and array, plus their instance fields
SharedOne private stack per threadOne heap shared by all threads
AllocationPush a frame on call, pop on returnAllocated by new, reclaimed by the collector
SizeSmall — around 512 KB to 1 MB per threadLarge — hundreds of megabytes to gigabytes
LifetimeExactly the duration of the method callUntil no reference can reach the object
SpeedVery fast — a pointer bumpSlower, and subject to collection pauses
When exhaustedStackOverflowErrorOutOfMemoryError
  • Holds

    StackMethod frames: locals, parameters, references, return address
    HeapEvery object and array, plus their instance fields
  • Shared

    StackOne private stack per thread
    HeapOne heap shared by all threads
  • Allocation

    StackPush a frame on call, pop on return
    HeapAllocated by new, reclaimed by the collector
  • Size

    StackSmall — around 512 KB to 1 MB per thread
    HeapLarge — hundreds of megabytes to gigabytes
  • Lifetime

    StackExactly the duration of the method call
    HeapUntil no reference can reach the object
  • Speed

    StackVery fast — a pointer bump
    HeapSlower, and subject to collection pauses
  • When exhausted

    StackStackOverflowError
    HeapOutOfMemoryError

Private and automatic versus shared and collected. Those two differences explain everything else.

Placing a value

Java
Loading compiler…

The rule, stated precisely

  • A primitive local stores its value directly in the frame.
  • A reference local stores an address in the frame; the object it names is on the heap.
  • Instance fields live inside the heap object, whatever their type — an int field is on the heap.
  • Static fields live with the class, in metaspace, and are reachable from every thread.
  • The frame disappears the moment the method returns. The objects it referenced survive if anything else still reaches them.

The rest of this guide is part of Pro

Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.

Still to come · 5 sections

  • Frames and the call chain
  • Why thread safety exists
  • Escape analysis
  • Reading the two errors
  • Common misreadings

Test yourself

Answer these before moving on — recall is what makes it stick.