Exercise 7: Big-Boned Goroutines - Starting With Huge Stacks
Want to learn more? Read The Go Runtime Scheduler on Internals for Interns for a deep dive into how Go’s runtime manages goroutines.
In this exercise, you’ll change the minimum goroutine stack size from 2KB to 1MB. You’ll see firsthand why Go’s small initial stacks are a key design decision — and what happens when you throw that away.
Learning Objectives
By the end of this exercise, you will:
- Understand how goroutine stacks differ from OS thread stacks
- Know how Go grows stacks using
morestackand contiguous copying - Find and modify the
stackMinconstant in the runtime - Observe the dramatic memory impact of large initial stacks at scale
Introduction: Goroutine Stacks vs OS Thread Stacks
One of Go’s most important properties is that you can launch thousands (even millions) of goroutines. This is possible because goroutines do not use OS thread stacks.
A typical OS thread allocates 1–8 MB of stack space upfront. With 10,000 threads, that’s 10–80 GB of virtual memory just for stacks — completely impractical.
Go solves this with small, growable goroutine stacks:
- Small start: Every goroutine starts with just 2KB of stack (the
stackMinconstant). - On-demand growth: When a function call would overflow the stack, the runtime runs
morestack, which allocates a larger stack and copies the entire current stack to the new location. - Contiguous stacks: Go uses a single contiguous block for each goroutine’s stack. When it grows, the whole stack moves. This is simpler and cache-friendlier than the old segmented stack approach (which Go abandoned in 1.3).
- Stack shrinking: After a GC cycle, stacks that are mostly empty can be shrunk back down to conserve memory.
The morestack mechanism adds a small overhead on function calls when the stack is near its limit, but for most programs this is negligible. The payoff is that 10,000 goroutines with 2KB stacks only use 20MB of memory.
The Key Constant
go/src/runtime/stack.go:
stackMin = 2048 // minimum goroutine stack size (2 KB)
This is the value we’ll change.
Step 1: Understand the Current Behavior
Create stack_demo.go to measure goroutine memory usage at the current default:
package main
import (
"fmt"
"runtime"
"sync"
)
func idleGoroutine(wg *sync.WaitGroup) {
wg.Done()
// Block so the goroutine stays alive
select {}
}
func measureGoroutines(n int) {
var memBefore, memAfter runtime.MemStats
runtime.GC()
runtime.ReadMemStats(&memBefore)
var wg sync.WaitGroup
wg.Add(n)
for i := 0; i < n; i++ {
go idleGoroutine(&wg)
}
wg.Wait()
runtime.GC()
runtime.ReadMemStats(&memAfter)
heapDelta := int64(memAfter.HeapInuse) - int64(memBefore.HeapInuse)
stackDelta := int64(memAfter.StackInuse) - int64(memBefore.StackInuse)
fmt.Printf("Goroutines: %d\n", n)
fmt.Printf(" Heap delta: %d KB\n", heapDelta/1024)
fmt.Printf(" Stack delta: %d KB\n", stackDelta/1024)
fmt.Printf(" Stack/goroutine: ~%d bytes\n", stackDelta/int64(n))
}
func main() {
measureGoroutines(1000)
}
Run with the standard Go:
go run stack_demo.go
Expected output (approximately):
Goroutines: 1000
Heap delta: 160 KB
Stack delta: 2048 KB
Stack/goroutine: ~2048 bytes
Each goroutine uses about 2KB of stack. 1000 goroutines use about 2MB. Very efficient.
Step 2: Find the stackMin Constant
grep -n "stackMin" go/src/runtime/stack.go | head -5
You’ll find it near the top of the file, in a const block:
const (
// ...
stackMin = 2048
// ...
)
This is the minimum (and initial) stack size for every new goroutine.
Step 3: Make Goroutines Big-Boned
Edit go/src/runtime/stack.go:
Change stackMin from 2048 to 1MB:
// Original:
stackMin = 2048
// Change to:
stackMin = 1 * 1024 * 1024 // 1 MB — big-boned goroutines
Why This Works (and Why It’s Expensive)
With stackMin = 1MB:
- Every new goroutine immediately allocates a 1MB stack
- The
morestackpath is almost never triggered — 1MB is enough for virtually any call chain - There is zero
morestackoverhead. No stack checks ever fire. - But: 1000 goroutines now use 1000 × 1MB = 1GB of memory
Step 4: Rebuild the Go Toolchain
cd go/src
./make.bash
Step 5: Observe the Memory Impact
Run the same stack_demo.go with your modified Go:
../go/bin/go run stack_demo.go
Expected output (approximately):
Goroutines: 1000
Heap delta: 160 KB
Stack delta: 1048576 KB
Stack/goroutine: ~1048576 bytes
Each goroutine now uses 1MB of stack. 1000 goroutines use about 1GB. The heap usage is identical — the change only affects the stack allocator.
Step 6: The Upside — No morestack Overhead
The one real benefit of huge starting stacks: functions never need to check for stack overflow, so the morestack hot path is never triggered. Create deep_recursion.go to see this:
package main
import (
"fmt"
"time"
)
// A deeply recursive function that would normally trigger morestack many times
func recurse(depth int) int {
if depth == 0 {
return 0
}
// Force some stack usage
var buf [256]byte
buf[0] = byte(depth)
return int(buf[0]) + recurse(depth-1)
}
func main() {
const maxDepth = 50_000
start := time.Now()
result := recurse(maxDepth)
elapsed := time.Since(start)
fmt.Printf("Result: %d\n", result)
fmt.Printf("Time for %d recursive calls: %v\n", maxDepth, elapsed)
}
Run with modified Go:
../go/bin/go run deep_recursion.go
Run with stock Go:
go run deep_recursion.go
With the 1MB stack, deep recursion is slightly faster because the morestack overhead is gone. But this is a small benefit that comes at enormous memory cost.
Understanding the Trade-Off
| Config | Stack/goroutine | 1,000 goroutines | 10,000 goroutines | morestack |
|---|---|---|---|---|
| Default (2KB) | 2 KB | 2 MB | 20 MB | Yes, when needed |
| Big-boned (1MB) | 1 MB | 1 GB | 10 GB | Never |
The 2KB default is a deliberately conservative choice that enables Go’s signature feature: cheap goroutines. The runtime grows stacks on demand, so you only pay for stack space you actually use.
Cleanup
To restore the original stack size:
cd go/src/runtime
git checkout stack.go
cd ..
./make.bash
Summary
You’ve given Go’s goroutines a lot more starting room:
Before: stackMin = 2048 → 1,000 goroutines use 2 MB of stack
After: stackMin = 1MB → 1,000 goroutines use 1,000 MB of stack
Changes: runtime/stack.go stackMin constant
Result: morestack never fires, but goroutines are 500x heavier
The Go designers chose 2KB carefully. It’s small enough to be cheap, large enough that morestack only triggers for deeply nested code. This exercise shows exactly what you lose when you abandon that choice.
Continue to Exercise 8 or return to the main workshop