Skip to content

Coroutine example does not run out of memory when run with threads, despite the docs saying that it should #2135

Closed
@npryce

Description

@npryce

The following is from the Kotlin coroutine docs:

Coroutines ARE light-weight

Run the following code:

import kotlinx.coroutines.*

fun main() = runBlocking {
    repeat(100_000) { // launch a lot of coroutines
        launch {
            delay(1000L)
            print(".")
        }
    }
}

It launches 100K coroutines and, after a second, each coroutine prints a dot.

Now, try that with threads. What would happen? (Most likely your code will produce some sort of out-of-memory error)

What happens is that it does not crash. (I'm using MacOS 10.15.5 on a 2020 13" MacBook Pro.)

The delay and/or coroutine count should be increased in the example code.

Metadata

Metadata

Assignees

Labels

docsKDoc and API reference

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions