From d0ec9a7b01dde637f639d7b04689f129037f28c3 Mon Sep 17 00:00:00 2001 From: scottolsen Date: Tue, 29 Mar 2022 10:14:46 -0400 Subject: [PATCH 1/2] docs: clarify section on coroutine memory consumption 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. --- docs/topics/coroutines-basics.md | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/docs/topics/coroutines-basics.md b/docs/topics/coroutines-basics.md index 885c29fc28..ac19b6eb03 100644 --- a/docs/topics/coroutines-basics.md +++ b/docs/topics/coroutines-basics.md @@ -245,24 +245,29 @@ Done -## 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 +} ``` +{kotlin-runnable="true" kotlin-min-compiler-version="1.3"} > You can get the full code [here](../../kotlinx-coroutines-core/jvm/test/guide/example-basic-06.kt). > @@ -270,10 +275,9 @@ fun main() = runBlocking { -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. From 1c85bfad35fbdc1f0fefbfbe3167c9231299afde Mon Sep 17 00:00:00 2001 From: scottolsen Date: Wed, 30 Mar 2022 10:12:10 -0400 Subject: [PATCH 2/2] docs: update coroutines-basics.md - 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. --- docs/topics/coroutines-basics.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/docs/topics/coroutines-basics.md b/docs/topics/coroutines-basics.md index ac19b6eb03..68ae97886f 100644 --- a/docs/topics/coroutines-basics.md +++ b/docs/topics/coroutines-basics.md @@ -251,23 +251,22 @@ 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 -('.'): +('.') while consuming very little memory: ```kotlin import kotlinx.coroutines.* fun main() = runBlocking { -//sampleStart repeat(100_000) { // launch a lot of coroutines launch { delay(5000L) print(".") } } -//sampleEnd } ``` -{kotlin-runnable="true" kotlin-min-compiler-version="1.3"} + > You can get the full code [here](../../kotlinx-coroutines-core/jvm/test/guide/example-basic-06.kt). >