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
docs: clarify section on coroutine memory consumption #3225
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -245,35 +245,39 @@ Done | |
|
||
<!--- TEST --> | ||
|
||
## Coroutines ARE light-weight | ||
## Coroutines are light-weight | ||
|
||
Run the following code: | ||
Coroutines are less resource-intensive than JVM threads. Code that exhausts the | ||
JVM's available memory when using threads can be expressed using coroutines | ||
without hitting resource limits. For example, the following code launches | ||
100000 distinct coroutines that each wait 5 seconds and then print a period | ||
('.'): | ||
|
||
```kotlin | ||
import kotlinx.coroutines.* | ||
|
||
//sampleStart | ||
fun main() = runBlocking { | ||
//sampleStart | ||
repeat(100_000) { // launch a lot of coroutines | ||
launch { | ||
delay(5000L) | ||
print(".") | ||
} | ||
} | ||
} | ||
//sampleEnd | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given that the code is not runnable, I think a better choice would be to remove There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done, thanks! |
||
``` | ||
{kotlin-runnable="true" kotlin-min-compiler-version="1.3"} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would go against our narrative :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah, I figured something like this might be the case--I added a comment noting as much so that future contributors don't make the same mistake, I felt there was probably a good reason for this one not being runnable but didn't see any immediate indication as to why, thus the PR--thanks for letting me know! |
||
|
||
> You can get the full code [here](../../kotlinx-coroutines-core/jvm/test/guide/example-basic-06.kt). | ||
> | ||
{type="note"} | ||
|
||
<!--- TEST lines.size == 1 && lines[0] == ".".repeat(100_000) --> | ||
|
||
It launches 100K coroutines and, after 5 seconds, each coroutine prints a dot. | ||
|
||
Now, try that with threads (remove `runBlocking`, replace `launch` with `thread`, and replace `delay` with `Thread.sleep`). | ||
What would happen? (Most likely your code will produce some sort of out-of-memory error) | ||
If you write the same program using threads (remove `runBlocking`, replace | ||
`launch` with `thread`, and replace `delay` with `Thread.sleep`), it will | ||
likely consume too much memory and throw an out-of-memory error. | ||
|
||
<!--- MODULE kotlinx-coroutines-core --> | ||
<!--- INDEX kotlinx.coroutines --> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or something in this vein. Otherwise, the whole point is obscured.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point! Added.