Skip to content
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

Merged
merged 2 commits into from Mar 31, 2022

Conversation

scolsen
Copy link
Contributor

@scolsen scolsen commented Mar 29, 2022

Updates the section "Coroutines are light-weight" to clarify its
comparison of the resource-intensiveness of coroutines and threads. The
content is much the same, I've attempted to make it follow the pattern
of the other sections by introducing the conceptual point first, then
presenting the code sample.

This commit also makes the code sample in this section runnable and
omits the surrounding runBlocking block.

Updates the section "Coroutines are light-weight" to clarify its
comparison of the resource-intensiveness of coroutines and threads. The
content is much the same, I've attempted to make it follow the pattern
of the other sections by introducing the conceptual point first, then
presenting the code sample.

This commit also makes the code sample in this section runnable and
omits the surrounding runBlocking block.
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
('.'):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
('.'):
('.'), all the while consuming very little memory:

Or something in this vein. Otherwise, the whole point is obscured.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! Added.

```
{kotlin-runnable="true" kotlin-min-compiler-version="1.3"}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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!

//sampleEnd
}
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 //sampleStart and //sampleEnd, given how they aren't recognized for code that is not runnable: https://kotlinlang.org/docs/coroutines-basics.html#coroutines-are-light-weight

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, thanks!

- Add some additional text that emphasizes that the example code has a
  small memory footprint.
- Add a note that we should not make the example runnable (since it will
  exhaust the playground heap)
- Remove //sample(Start|End) comments since they are not recognized for
  code that is not runnable.
@dkhalanskyjb dkhalanskyjb merged commit f991742 into Kotlin:develop Mar 31, 2022
@dkhalanskyjb
Copy link
Collaborator

Thanks!

pablobaxter pushed a commit to pablobaxter/kotlinx.coroutines that referenced this pull request Sep 14, 2022
Updates the section "Coroutines are light-weight" to clarify its
comparison of the resource-intensiveness of coroutines and threads. The
content is much the same.

This commit also makes the code sample in this section runnable and
omits the surrounding runBlocking block.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants