Coroutines vs. Threads: The True Cost of a Context Switch
summary
Explains OS and hardware costs of context switches down to syscalls, kernel traps, TCB bookkeeping and CPU cache invalidation, measures native memory with Android Studio Profiler, shows threads require 1–2MB thread stacks and repeatedly cause cache-thrashing, demonstrates the runtime launching 100,000 coroutines (small continuation objects of a few hundred bytes) while the same app crashes after a few hundred threads, and confirms coroutines run in user space so suspension avoids costly kernel context switches.
AI-written summary · about
chapters (13)
- 0:00Introduction and agenda
- 1:45Thread fundamentals and overhead
- 6:22Context switches and cache misses
- 13:49Coroutine lightweight model
- 18:26Dispatchers and scheduling
- 20:18Live memory and performance demo
- 24:14Thread vs coroutine comparison
- 25:17Structured concurrency benefits
- 27:46Choosing the right dispatcher
- 31:39Main thread and UI responsiveness
- 32:42Real-world dispatcher examples
- 34:52User space advantages summary
- 36:17Q&A and closing
