Tuan Kiet
09/21/2019, 8:58 AMscope.launch(<http://Dispatchers.IO|Dispatchers.IO>) {
doSomeWork() // take 1 second
}
does using coroutine create memory footprint? because it has to use Thread
at some point right? If I just launch 1 coroutine at the beginning and my app stay alive for 3 hours, does that Thread
stay in memory for 3 hours?Dennis Schröder
09/21/2019, 9:09 AMTuan Kiet
09/21/2019, 9:51 AMCoroutines uses a threadpool
Can we have a reference on this, from the source code maybe?Dominaezzz
09/21/2019, 11:59 AM<http://Dispatchers.IO|Dispatchers.IO>
Dennis Schröder
09/21/2019, 12:36 PMTuan Kiet
09/21/2019, 3:47 PMshared pool of threads
does not go away once it got created?Pablichjenkov
09/21/2019, 3:52 PMgroostav
09/21/2019, 9:48 PMDispatchers
.Tuan Kiet
09/21/2019, 11:57 PMThread-pools define behaviour around "if we have work create needed threads, if we have no work release as many threads to the GC as possible"
can we get a reference on this?Tuan Kiet
09/22/2019, 12:26 AM<http://Dispatchers.IO|Dispatchers.IO>
-> <http://DefaultScheduler.IO|DefaultScheduler.IO>
-> ExperimentalCoroutineDispatcher
which have idleWorkerKeepAliveNs
which is
@JvmField
internal val IDLE_WORKER_KEEP_ALIVE_NS = TimeUnit.SECONDS.toNanos(
systemProp("kotlinx.coroutines.scheduler.keep.alive.sec", 5L)
)
while Worker is a Thread
internal inner class Worker private constructor() : Thread()
and
private val workers: Array<Worker?> = arrayOfNulls(maxPoolSize + 1)
and
/*
* 5) It is safe to clear reference from workers array now.
*/
workers[lastIndex] = null
I suspect this is where coroutine implementation try to release unused thread so the system can GC it away. I’m correct?Tuan Kiet
09/22/2019, 1:10 AMnull
will make it eligible to GC?Dominaezzz
09/22/2019, 1:13 AMDominaezzz
09/22/2019, 1:14 AMKroppeb
09/22/2019, 6:18 AM<http://Dispatchers.IO|Dispatchers.IO> – uses a shared pool of on-demand created threads and is designed for offloading of IO-intensive blocking operations (like file I/O and blocking socket I/O).
This means the thread pool isn't a constant size right?Tuan Kiet
09/22/2019, 7:30 AMgroostav
09/22/2019, 8:52 PM> Thread-pools define behaviour around "if we have work create needed threads, if we have no work release as many threads to the GC as possible"
can we get a reference on this?I mean, this is more a jvm thing than a kotlin thing. I think a good start place would be https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Executors.html. Reading the source code will likely be difficult because the Fork-join pool for example implements a pretty sohpshticated work sharing system. @Tuan Kiet are you new to both java threading and coroutines or just coroutines? I think its fair to say that kotlinx.coroutines uses a pretty safe, direct, and intuitive mapping of the exsiting conventions around threads from the JVM, with a few caveats.
groostav
09/22/2019, 8:53 PMTuan Kiet
09/23/2019, 12:57 AM