DEV Community

Discussion on: Coroutines, key Concepts

Collapse
 
jbc7ag profile image
Jess Barrientos

Basically, every Thread has its own stack. According to the documentation, 64k is the least amount of stack space allowed per thread in the JVM while a simple coroutine in Kotlin occupies only a few dozen bytes of heap memory.

Try to run this example and see It on your own. Then, change it to "normal" Threads and see how your app crash ahah

fun main(){
    runBlocking {
        repeat(1_000_000){
            launch {
                print(".")
            }
        }
    }
} 
Enter fullscreen mode Exit fullscreen mode
Collapse
 
andreidascalu profile image
Andrei Dascalu

Every thread has a stack, that's the definition of a thread (well, that plus registers, counters and a couple other things). What makes this lightweight?