dragon456101 2017-04-19 11:03
浏览 65
已采纳

当匿名函数在golang中永远等待通道时会发生什么?

I have a function creating a channel with no buffer. This function goes on to create several other concurrent anonymous functions writing to said channel. The function then goes on to wait for input on the channel and then returns the value.

See example below

package main

import (
    "time"
    "fmt"
    "strconv"
    "math/rand"
)

func main() {
    for{
        text := foo()
        fmt.Println(text)
        time.Sleep(time.Second)
    }

}

func foo() string {    
    ch := make(chan string)
    for i := 0; i < 10; i++ {
        // Create some threads
        go func(i int) {
            time.Sleep(time.Duration(rand.Intn(1000))*time.Millisecond)
            ch <- strconv.Itoa(i)
        }(i)
    }
    return <- ch
}

What happens with the anonymous functions that are still waiting on the channel, even though the entire function (foo in example) is "dead"?

Will they be collected as garbage, or will they forever wander the limbo of my computers memory (or until I kill the main thread) eating away at it in a desperate attempt to send their last message before passing on?

  • 写回答

1条回答 默认 最新

  • duankui6150 2017-04-19 11:33
    关注

    You have a goroutine leak.

    package main
    
    import (
        "fmt"
        "math/rand"
        "runtime"
        "strconv"
        "time"
    )
    
    func main() {
        for {
            text := foo()
            fmt.Println(text, "NumGoroutine", runtime.NumGoroutine())
            time.Sleep(time.Second)
        }
    }
    
    func foo() string {
        ch := make(chan string)
        for i := 0; i < 10; i++ {
            // Create some threads
            go func(i int) {
                time.Sleep(time.Duration(rand.Intn(1000)) * time.Millisecond)
                ch <- strconv.Itoa(i)
            }(i)
        }
        return <-ch
    }
    

    Output:

    $ go run leak.go
    4 NumGoroutine 10
    4 NumGoroutine 20
    0 NumGoroutine 28
    8 NumGoroutine 37
    2 NumGoroutine 46
    8 NumGoroutine 55
    2 NumGoroutine 64
    3 NumGoroutine 73
    8 NumGoroutine 82
    1 NumGoroutine 91
    4 NumGoroutine 100
    <<--SNIP-->>
    4 NumGoroutine 4006
    7 NumGoroutine 4015
    6 NumGoroutine 4024
    9 NumGoroutine 4033
    9 NumGoroutine 4042
    9 NumGoroutine 4051
    1 NumGoroutine 4060
    0 NumGoroutine 4069
    4 NumGoroutine 4078
    0 NumGoroutine 4087
    6 NumGoroutine 4096
    ^Csignal: interrupt
    $
    

    Like a memory leak, a goroutine leak is bad.

    For some background, see Go: proposal: runtime: garbage collect goroutines blocked forever #19702: Closed.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛