为什么我会出现内存泄漏?

huangapple go评论68阅读模式
英文:

Why am I getting a memory leak?

问题

我有以下代码:http://play.golang.org/p/1aaive8KQx

当我打印runtime.NumGoroutine()时,我得到3。我不应该只得到1吗?为什么?

package main

import (
	"log"
	"runtime"
	"time"
)

func main() {
	for i := 1; i <= 10; i++ {
		ch := make(chan int, 10)
		timeout := time.Tick(1 * time.Second)
		for i := 1; i <= 10; i++ {
			go func(i int) {
				time.Sleep(2 * time.Second)
				ch <- i
			}(i)
		}

		for i := 1; i <= 10; i++ {
			select {
			case j := <-ch:
				log.Println(j)
			case <-timeout:
				log.Println("timeout")

			}

		}

		log.Println("Processes", runtime.NumGoroutine())
	}
}
英文:

I have the following: http://play.golang.org/p/1aaive8KQx

When I print the runtime.NumGoroutine() I get 3. Shouldn't I be getting just 1? Why?

package main

import (
	&quot;log&quot;
	&quot;runtime&quot;
	&quot;time&quot;
)

func main() {
	for i := 1; i &lt;= 10; i++ {
		ch := make(chan int, 10)
		timeout := time.Tick(1 * time.Second)
		for i := 1; i &lt;= 10; i++ {
			go func(i int) {
				time.Sleep(2 * time.Second)
				ch &lt;- i
			}(i)
		}

		for i := 1; i &lt;= 10; i++ {
			select {
			case j := &lt;-ch:
				log.Println(j)
			case &lt;-timeout:
				log.Println(&quot;timeout&quot;)

			}

		}

		log.Println(&quot;Processes&quot;, runtime.NumGoroutine())
	}
}

答案1

得分: 1

有一个奇怪的竞态条件。基本上,当你调用Println时,一些goroutine仍在运行,但很快就会终止。在Println之前放一个睡眠,你会得到1个进程。如果你读取日志,你会看到2个超时 - 这意味着你在循环中跳过了2次通道读取。不知何故,它给了你的主goroutine时间从通道中读取8个值并在2个goroutine终止之前调用Println。这是一个竞态条件,所以很难准确描述发生了什么。除了你的代码之外,调度器和通道的实现也在这里起着重要作用。

英文:

There is a weird race condition. Basically what happens is when you call Println some goroutines are still running but will terminate shortly. Put a sleep before Println and you will get 1 Processes. If you read the log you will see 2 timeouts - it means you skipped 2 channel reads in the loop. Somehow it gives your main goroutine time to read 8 values from the channel and call Println before 2 goroutines are terminated. It's a race condition so it's hard to describe exactly what's going on. Apart from your code the scheduler and channels implementation also play major here.

huangapple
  • 本文由 发表于 2016年2月28日 09:39:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/35677713.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定