memory-leaks - 垃圾收集器会收集永远不会继续的 Go 例程吗?

标签 memory-leaks go goroutine

将以下代码视为一个简化示例:

func printer(c <-chan int) {
    for {
        fmt.Print(<-c)
    }
}

func provide() {
    c := make(chan int)

    go printer(c)

    for i := 1; i <= 100; i++ {
        c <- i
    }
}

provide 函数创建了一个 go 例程 printer,用于打印 provide 生成的数据。

我的问题是,在 provide 返回并且 printer 开始阻塞空 channel 后会发生什么。 go 例程是否会泄漏,因为没有进一步引用 c 还是垃圾收集器会捕获这种情况并处理 go 例程和 c

如果确实是这种代码导致内存泄漏,我可以采取什么策略来防止这种内存泄漏的发生?

最佳答案

关闭 channel 。从关闭的 channel 读取始终成功,并返回相应的零值。可选的第二个 bool 返回值表示第一个值的有效性。

Receive operator :

A receive expression used in an assignment or initialization of the form

x, ok = <-ch
x, ok := <-ch
var x, ok = <-ch

yields an additional result of type bool reporting whether the communication succeeded. The value of ok is true if the value received was delivered by a successful send operation to the channel, or false if it is a zero value generated because the channel is closed and empty.

func printer(c <-chan int) {
        for {
                v, ok := <-c
                if !ok { // chan closed
                        return
                }

                // v is valid
                fmt.Println(v)
        }
}

func provide() {
        c := make(chan int)

        go printer(c)

        for i := 1; i <= 100; i++ {
                c <- i
        }
        close(c)
}

关于memory-leaks - 垃圾收集器会收集永远不会继续的 Go 例程吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16591117/

相关文章:

javascript - 如果附加了事件,删除 DOM 对象(在 Javascript 中)是否会导致内存泄漏?

Go struct 方法允许类型混合?

windows - 是否有更好的客户端来查看系统监视器日志?

go - 如何找到 Go 模块源缓存?

google-app-engine - Go + App Engine 数据存储区 : How to filter out rows that are null?

go - 处理 goroutine 时的奇怪事情

c++ - 分段堆栈如何工作

go - 一个关于 Go Channel 的死锁及其原因的简单示例

java - 内存泄漏总是需要一个长生命周期的对象?

c++ - 在 C++ Builder XE4 中搜索内存泄漏