Zombie goroutines in Go

三世轮回 提交于 2020-02-29 06:24:17

问题


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?


回答1:


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.

If a goroutine never returns (for example because it contains an infinite for {} with no breaks or returns) 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.

For more details, read the thread at https://groups.google.com/forum/#!topic/golang-nuts/uiySuH8_3Y4



来源:https://stackoverflow.com/questions/23854900/zombie-goroutines-in-go

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!