Golang Goroutine泄漏

时间:2015-03-22 02:35:52

标签: memory-leaks go goroutine

// Copyright 2012 The Go Authors.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build ignore

package main

import (
    "fmt"

    "code.google.com/p/go-tour/tree"
)

func walkImpl(t *tree.Tree, ch chan int) {
    if t == nil {
        return
    }
    walkImpl(t.Left, ch)
    ch <- t.Value
    walkImpl(t.Right, ch)
}

// Walk walks the tree t sending all values
// from the tree to the channel ch.
func Walk(t *tree.Tree, ch chan int) {
    walkImpl(t, ch)
    // Need to close the channel here
    close(ch)
}

// Same determines whether the trees
// t1 and t2 contain the same values.
// NOTE: The implementation leaks goroutines when trees are different.
// See binarytrees_quit.go for a better solution.
func Same(t1, t2 *tree.Tree) bool {
    w1, w2 := make(chan int), make(chan int)

    go Walk(t1, w1)
    go Walk(t2, w2)

    for {
        v1, ok1 := <-w1
        v2, ok2 := <-w2
        if !ok1 || !ok2 {
            return ok1 == ok2
        }
        if v1 != v2 {
            return false
        }
    }
}

func main() {
    fmt.Print("tree.New(1) == tree.New(1): ")
    if Same(tree.New(1), tree.New(1)) {
        fmt.Println("PASSED")
    } else {
        fmt.Println("FAILED")
    }

    fmt.Print("tree.New(1) != tree.New(2): ")
    if !Same(tree.New(1), tree.New(2)) {
        fmt.Println("PASSED")
    } else {
        fmt.Println("FAILED")
    }
}

在此代码中,http://tour.golang.org/concurrency/8

的解决方案

为什么有关于Same()函数的注释相同(t1,t2 * tree.Tree)bool说它泄漏了goroutines?怎么会这样?它还提到了第二个修复此问题的文件:

// Copyright 2015 The Go Authors.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build ignore

package main

import (
    "fmt"

    "code.google.com/p/go-tour/tree"
)

func walkImpl(t *tree.Tree, ch, quit chan int) {
    if t == nil {
        return
    }
    walkImpl(t.Left, ch, quit)
    select {
    case ch <- t.Value:
        // Value successfully sent.
    case <-quit:
        return
    }
    walkImpl(t.Right, ch, quit)
}

// Walk walks the tree t sending all values
// from the tree to the channel ch.
func Walk(t *tree.Tree, ch, quit chan int) {
    walkImpl(t, ch, quit)
    close(ch)
}

// Same determines whether the trees
// t1 and t2 contain the same values.
func Same(t1, t2 *tree.Tree) bool {
    w1, w2 := make(chan int), make(chan int)
    quit := make(chan int)
    defer close(quit)

    go Walk(t1, w1, quit)
    go Walk(t2, w2, quit)

    for {
        v1, ok1 := <-w1
        v2, ok2 := <-w2
        if !ok1 || !ok2 {
            return ok1 == ok2
        }
        if v1 != v2 {
            return false
        }
    }
}

func main() {
    fmt.Print("tree.New(1) == tree.New(1): ")
    if Same(tree.New(1), tree.New(1)) {
        fmt.Println("PASSED")
    } else {
        fmt.Println("FAILED")
    }

    fmt.Print("tree.New(1) != tree.New(2): ")
    if !Same(tree.New(1), tree.New(2)) {
        fmt.Println("PASSED")
    } else {
        fmt.Println("FAILED")
    }
}

它是如何实现的?泄漏在哪里? (要测试代码,您必须在http://tour.golang.org/concurrency/8上运行它)。非常困惑,并希望得到一些帮助,谢谢!

2 个答案:

答案 0 :(得分:2)

当检测到差异时,程序停止在频道上接收。

walk goroutines一直运行,直到阻止发送到频道。他们永远不会退出这是泄漏。

答案 1 :(得分:0)

第二个解决方案使用退出通道处理泄漏。当退出通道关闭时(在Same()函数中),select语句的情况2成功(在walkImpl函数中为case&lt; -quit)并且函数返回。因此,在程序退出后,walkImpl函数中没有阻塞。