Contents

Stack vs Heap in Go

When it comes to programming in Go, understanding the differences between stack and heap is crucial for efficient memory management.

What is the Stack?

The stack is a per-goroutine call stack that stores function parameters, return addresses, and non-escaping local variables. The Go runtime starts each goroutine with a small stack (typically 2KB) and grows or shrinks it dynamically as needed. Stack frames are reclaimed immediately when a function returns, placing virtually zero pressure on the garbage collector.

Characteristics:

  • Automatic frame-based allocation and reclamation with minimal overhead
  • Managed per goroutine with dynamic growth and shrinking
  • Holds values whose lifetime stays strictly within the function scope

What is the Heap?

The heap is a shared dynamic memory region accessible across goroutines. Unlike C or C++, Go has no manual heap deallocation — whether a value lives on the stack or the heap is decided by the Go compiler’s escape analysis, not by whether you called new() or make():

  • Even if you allocate with new() or make(), the compiler can place the object on the stack if it proves the reference never outlives the function.
  • Conversely, a plain local variable escapes to the heap if its pointer is returned, stored in a global, or boxed into an interface in a way the compiler cannot bound to the stack frame. Heap allocations are reclaimed automatically by Go’s garbage collector (GC).

Characteristics:

  • Placement decided by compiler escape analysis; reclaimed automatically by Go’s GC (no manual free)
  • Lifetime can outlive the creating function and be shared across goroutines
  • Higher allocation and GC-scanning cost than stack frames, making hot-path escapes worth measuring

Key Differences

Lifetime

  • Stack: Tied to the function call frame; reclaimed immediately on return.
  • Heap: Can outlive the creating function until unreachable, then reclaimed automatically by the GC.

Memory Management

  • Stack: Pushed on call and popped on return with no GC involvement.
  • Heap: Managed by the Go runtime allocator and reclaimed automatically by the concurrent GC.

Sizing and Growth

  • Stack: Starts small (~2KB per goroutine) and grows or shrinks automatically at runtime.
  • Heap: Process-wide shared pool that requests memory from the OS on demand and reuses freed spans via GC.

Inspecting and Reducing Unnecessary Heap Escapes

You cannot manually force stack vs. heap placement in Go, but you can inspect the compiler’s escape decisions directly:

go build -gcflags="-m" ./...

Practical guidelines:

  • Write clear, idiomatic Go first — avoid guessing about pointer vs. value semantics without benchmark or pprof evidence.
  • Watch hot paths: returning pointers to local structs, boxing values into any/interfaces, and slices with unknown compile-time sizes are common escape triggers.
  • Pair go test -bench=. -benchmem (allocs/op, B/op) with pprof so you only optimize allocations that actually drive GC overhead.

Verdict

Understanding stack vs. heap in Go is not about “choosing manual heap management” — it is about understanding how escape analysis and automatic GC work together:

  • The stack holds non-escaping, function-scoped values at near-zero cost.
  • The heap holds escaping or shared values managed automatically by the GC; use go build -gcflags="-m" and pprof to verify where allocations actually happen.

References