Coroutines vs. Threads: The True Cost of a Context Switch
summary
Explains that crossing the user→kernel boundary triggers a syscall and kernel trap where the OS saves registers and TCBs, flushing CPU caches; a raw thread carries a 1–2 MB thread stack and kernel-managed overhead, so a few hundred threads exhaust memory and cause cache-miss storms. Coroutines save tiny continuation objects on the heap (a few hundred bytes), use cooperative scheduling, and avoid context switches; a live demo with Android Studio Profiler contrasts crashing at hundreds of threads versus launching 100,000 coroutines quickly and with low memory.
AI-written summary · about
chapters (8)
- 0:00Coroutines and context switches
- 7:05Threads are inefficient
- 13:02Coroutines avoid kernel overhead
- 16:58Live demo: coroutines vs threads
- 25:14Structured concurrency
- 31:14Choosing dispatcher
- 33:42Dispatcher selection scenarios
- 37:15Key takeaways
related talks
39mCoroutines vs. Threads: The True Cost of a Context Switch
33mUntangle your App: Efficient Threading in Android
36mJava vs. Kotlin: The Ultimate Showdown of Virtual Threads & Coroutines
30mExtra! Extra! Thread all about it!
42mDebugging Application Performance
28mMy coroutines patterns and habits, 3 years later
38mAndroid Suspenders
44mCoordinating Space and Time
