Go中的僵尸goroutines [英] Zombie goroutines in Go
问题描述
我一直想知道Go中的goroutine如何被杀死,直到我最近观看了
I was always wondering how could a goroutine in Go get killed until i recently watched A Channel Compendium by John Graham-Cumming at GoPherCon and realized that its as simple as having a return statement inside the code executed by goroutines. So the same goes for closures right? Shouldn't we always use return statements inside closures so every goroutine executing a closure terminates at some point? What are the disadvantages of having zombie goroutines in your system and how do they get terminated?
推荐答案
根据Jsor的评论,一个函数必须返回以结束goroutine,但是即使到达该函数的末尾,即使没有明确的return
语句.
As per Jsor's comment, a function must return to end a goroutine, but that can happen if it reaches the end of the function even if there isn't an explicit return
statement.
如果goroutine永不返回(例如,因为它包含无break
或return
s的无限for {}
)并且不再执行有用的工作,则基本上是内存泄漏;它永远不会终止,它只是坐下来并消耗资源,直到您的程序结束.
If a goroutine never returns (for example because it contains an infinite for {}
with no break
s or return
s) and is no longer doing useful work then it is basically a memory leak; it will never get terminated, and it simply sits and chews up resources until your program ends.
有关更多详细信息,请阅读 https://groups上的线程. google.com/forum/#!topic/golang-nuts/uiySuH8_3Y4
For more details, read the thread at https://groups.google.com/forum/#!topic/golang-nuts/uiySuH8_3Y4
这篇关于Go中的僵尸goroutines的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!