Chapter 12Concurrency Primitives MeasuredPaid chapter

Goroutines: Stack Growth, Scheduling Cost, and Preemption

Measure goroutine scheduling and stack memory, and test preemption with a negative control.

Launch paywall

This chapter is part of the paid launch. The preview stays visible so readers can evaluate the exact scope before buying.

Included in

Core and Complete. Complete also includes the practical exam and free Go-version updates.

Preview

What this chapter teaches

Common belief

Goroutines are free. Spawn as many as you want — millions, one per request, one per item — it costs nothing.

What actually happens

a goroutine is a g struct plus a stack that starts at 2 KiB and grows by allocating a bigger stack and copying the old one. Creating one costs a few hundred nanoseconds; each live one holds its live stack memory (so 10k idle goroutines ≈ tens of MiB); and deep call chains can grow stacks, whose memory is accounted for separately from heap objects. Cheap enough to use freely, expensive enough that unbounded goroutine creation is a real resource leak — which is why runtime.NumGoroutine() belongs on your dashboards.

Mechanisms covered
Live recursive frames can increase StackInuse; heap allocation profiles do not record stack copies.
The single-P spinner experiment completes with asynchronous preemption and times out in its disabled-preemption child.
benchexperiment
One-time purchase

Unlock Chapter 12 and the rest of the course.

Core unlocks every paid chapter and lab. Complete also includes the practical exam and future Go-version updates.

Core
$49

Full course access with the complete mechanics curriculum.

All paid chapters unlocked
One-time purchase
Perfect if you want the material only
Complete
$79
Best value

Best-value launch option with the exam and free Go-version course updates.

Everything in Core
Black Belt practical exam
Free Go-version updates

No subscription or account required. Access is tied to the email entered at Stripe Checkout and can be restored by email.

Free chapter
Escape Analysis: The Compiler's Fragile Decision
Distinguish escape diagnostics from measured allocations and explain the role of lifetime and call-site visibility.
Free chapter
Maps: Swiss Tables, Growth, and Permanent Memory
Distinguish map table capacity from referenced payloads and inspect clearing and growth on Go 1.25.