https://kotlinlang.org logo
#coroutines
Title
# coroutines
r

rocketraman

05/20/2020, 9:12 PM
Has anyone looked at / benchmarked the GC overhead of coroutine suspend and resume?
o

octylFractal

05/20/2020, 9:14 PM
GC overhead?
r

rocketraman

05/20/2020, 9:18 PM
Yeah. Not sure what's unclear about that.
Assuming a JVM backend...
o

octylFractal

05/20/2020, 9:18 PM
I'm just curious where you think it would come from
I don't think it has a significant impact on GC, as all the fields are contained in a single object so it doesn't create any sort of loop unless you already had one
r

rocketraman

05/20/2020, 9:20 PM
The Continuation object that represents the continuation has to be GCed after use. Is that the single object you refer to?
o

octylFractal

05/20/2020, 9:20 PM
yes
but that is not related to suspend/resume, it's not reallocated on resume -- it's only created on the call, and made GC'able after the call finishes
r

rocketraman

05/20/2020, 9:22 PM
Ok, so is "garbage created by suspend functions" better wording?
o

octylFractal

05/20/2020, 9:22 PM
yes
r

rocketraman

05/20/2020, 9:23 PM
A
Continuation
is an interface so without digging into the implemenation details its hard to say how big its reference tree can get. That's why I'm asking...
o

octylFractal

05/20/2020, 9:23 PM
it's as big as the variables in your coroutine
it's unique for each
suspend
function (see generated bytecode)
r

rocketraman

05/20/2020, 9:24 PM
+ whatever is in the CoroutineContext
o

octylFractal

05/20/2020, 9:24 PM
yes, though I would consider that a "variable in your coroutine"
r

rocketraman

05/20/2020, 9:25 PM
Yeah, but I mean, there is additional memory in the CoroutineContext that are not variables in my coroutine. For example, the job and coroutine name and etc. etc.
o

octylFractal

05/20/2020, 9:26 PM
yes
r

rocketraman

05/20/2020, 9:27 PM
So coming full circle... I don't know exactly what all that additional stuff is without digging deep so... what is the GC overhead of all that stuff?
o

octylFractal

05/20/2020, 9:27 PM
I'd say it's hard to measure
I suppose the best thing to do might be running a bunch of coroutines and seeing what happens to allocations, I personally have not done that
r

rocketraman

05/20/2020, 9:28 PM
Me either... hence the question 🙂
o

octylFractal

05/20/2020, 9:39 PM
personally I think it's so variable, but also so tiny, that you'd just have to measure your own application
there's certainly some overhead, but I don't think it's very significant, from a small test I did with millions of `delay`ing coroutines
r

rocketraman

05/20/2020, 9:41 PM
Yeah I'll probably throw a JMH benchmark together...
l

Luis Munoz

05/21/2020, 2:34 AM
Please share results when you do
3 Views