Goroutines vs. OS Threads: 500x Less Memory, 70x Faster Switch
Goroutines start with only ~2KB stack space, allowing millions to run with minimal memory, whereas OS threads typically require ~1MB (Linux default); context switching a goroutine is ~70 times faster (around 54ns) than an OS thread's ~3.5 microseconds.
The top 3
- Go's M:N Scheduler: The Concurrency Multiplier: Go's runtime employs an M:N scheduler, mapping M goroutines onto N operating system threads (where M is often much larger than N), drastically reducing the overhead compared to creating an OS thread for each concurrent task.
- Goroutine Stacks: Tiny Start, Dynamic Growth: Goroutines start with a minimal stack size, typically 2KB, and dynamically grow or shrink as needed, contrasting sharply with OS threads that often allocate fixed, larger stacks (e.g., 1MB) upfront, leading to significant memory savings.
- User-Space Switching: Goroutine's Speed Secret: Goroutine context switching happens entirely in user space, managed by the Go runtime, which is significantly faster (around 70x) than OS thread switching, which requires expensive kernel-level operations and system calls.
Sources
Open the full topic