Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
npryce opened this issue Jul 13, 2020 · 1 comment
Assignees
Labels
docs KDoc and API reference

Comments

@npryce
Copy link

npryce commented Jul 13, 2020

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.

@elizarov
Copy link
Contributor

See explanation here: https://medium.com/@elizarov/such-concurrency-many-threads-wow-7c81ba9e9ebe
The example needs to be updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs KDoc and API reference
Projects
None yet
Development

No branches or pull requests

2 participants