英文:
Do unstarted, discarded lazy asyncs cause a memory leak?
问题
Suppose I hold a reference to a CoroutineScope
, and create a large number of coroutines with async(CoroutineStart.LAZY) { ... }
. Suppose I also never use/await many or all of the resulting Deferred
s.
这会创建内存泄漏吗? CoroutineScope
会保持对未启动的子 Deferred 的引用吗? val Job.children : Sequence<Job>
的存在可能暗示着这一点,但也许在 NEW 和 ACTIVE 之间的状态转换中有一些我没有注意到的东西。我粗略地查看了源代码,但当然很难理解。
英文:
Suppose I hold a reference to a CoroutineScope
, and create a large number of coroutines with async(CoroutineStart.LAZY) { ... }
. Suppose I also never use/await many or all of the resulting Deferred
s.
Does this create a memory leak? Does the CoroutineScope
hold a reference to the unstarted child Deferreds? The presence of the member val Job.children : Sequence<Job>
suggests it might be, but perhaps there's something going on with the state transition between NEW and ACTIVE that I'm missing. I glanced at the source, but it's of course pretty hard to follow.
答案1
得分: 1
Experimental verification suggests that yes, unstarted asyncs do leak in that way. I could imagine ways that might be addressed, but at least today with coroutines 1.7.1, that is a strong reference that could result in leaks.
英文:
Experimental verification suggests that yes, unstarted asyncs do leak in that way. I could imagine ways that might be addressed, but at least today with coroutines 1.7.1, that is a strong reference that could result in leaks.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论