Goroutines vs. OS Threads: Context Switching Advantage

Go's scheduler performs goroutine context switches entirely in user space, making them significantly faster (around 50-200 nanoseconds) compared to kernel-level OS thread context switches (typically 1-10 microseconds), which involves more overhead and system calls.

The top 3

  1. Goroutine Context Switch Speed: Up to 70x Faster: Goroutines achieve context switching in as little as 50-200 nanoseconds, making them significantly faster than OS threads, which typically incur an overhead of 1-5 microseconds per switch due to kernel involvement.
  2. Memory Footprint: Goroutines' Tiny 2KB Stacks vs. OS Threads' Megabytes: Goroutines start with a minimal 2KB stack that dynamically grows only as needed, allowing Go programs to spawn millions of concurrent tasks. In contrast, OS threads typically allocate large, fixed-size stacks, often 1-8 MB, leading to significant memory consumption for high concurrency.
  3. User-Space Scheduling: Goroutines Bypass Kernel Overhead: Goroutines are scheduled in user space by the Go runtime, completely bypassing the costly kernel-level transitions and extensive state saving required for OS thread context switches, significantly reducing overhead.

Sources

Open the full topic