问题
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 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.
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