未启动、丢弃的惰性异步会导致内存泄漏吗?

huangapple go评论52阅读模式
英文:

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 Deferreds.

这会创建内存泄漏吗? 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 Deferreds.

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.

huangapple
  • 本文由 发表于 2023年6月29日 05:13:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76576737.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定