Who does the garbage collection work in go?

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

Who does the garbage collection work in go?

问题

根据这个规范,Go语言背后有一个标记-清除的垃圾回收机制。但是是谁来执行这个机制呢?

  • Go代码会编译成本地二进制文件,对吗?所以不会像Java那样有一个虚拟机来执行。那么是一个神秘的线程吗?还是只是一个goroutine?

  • 垃圾回收过程会像Java的全GC那样导致停顿吗?有人能告诉我Java和Go之间的GC机制的区别吗?我在网上很少找到相关资料。

英文:

According to this specification, there is a mark-and-sweep garbage collection mechanism behind go. But who does it?

  • Go code will compile to native binary, right? So there will not be a virtual machine like Java which it could rely on. So who does this dirty work for us? A cryptic thread? Or just a goroutine?

  • Will the garbage collecting procedure stop-the-world like a full GC of Java? And could any one tell the difference of GC mechanisms between Java and Go? I could find rarely material on the net.

答案1

得分: 3

你的许多问题在这里得到了回答:

至于其他问题:

> 但是是谁来做这个工作呢?

由Go实现提供的本地代码运行时库。

(我没有查看过实现,但很难想象你可以在Go语言的“上层”中实现一个高性能的GC。)

> Go代码会编译成本地二进制文件,对吗?

正确。Go的常见问题解答清楚地说明了这一点。

> 所以它不会像Java那样依赖于虚拟机。

正确。然而,这并没有什么区别。在Java的情况下,GC也是由Java运行时提供的本地代码库实现的。

> 那么是谁为我们做这些“脏活”呢?一个神秘的线程?还是只是一个goroutine?

从Go 1.1开始,GC是并行的,所以在幕后肯定有一些多线程操作。Goroutine是Go语言的概念,很难想象你会在本地代码的GC实现中在“上层”使用它们。(但我可能错了...)

但你还需要明白,goroutine在底层也涉及到多线程。常见问题解答中提到:

> “为什么我的多goroutine程序没有使用多个CPU?”
>
> “你必须设置GOMAXPROCS环境变量或使用runtime包中同名函数,以允许运行时支持利用多个操作系统线程。”

明白了吗?在底层涉及到本地/操作系统线程。

英文:

Many of your questions are answered here:

For the rest:

> But who does it?

Native code runtime libraries provided by the Go implementation.

(I haven't looked at the implementation, but it is hard to imagine that you could implement a high-performance GC for Go "above the line" in the Go language.)

> Go code will compile to native binary, right?

Correct. The Go FAQ says so clearly.

> So there will not be a virtual machine like Java which it could rely on.

Correct. However, that makes no difference. In the Java case, the GC is also implemented by native code libraries provided by the Java runtime.

> So who does this dirty work for us? A cryptic thread? Or just a goroutine?

Well from Go 1.1 onwards, the GC is parallel so there must be some kind of multi-threading going on behind the scenes. Goroutines are a Go language concept, and it is hard to image that you would use them "below the line" in the native code GC implementation. (But I could be wrong ...)

But you also need to understand that goroutines also entail multiple threads under the hood. The FAQ says:

> "Why doesn't my multi-goroutine program use multiple CPUs?
>
> You must set the GOMAXPROCS shell environment variable or use the similarly-named function of the runtime package to allow the run-time support to utilize more than one OS thread."

See? Native / OS threads are involved under the hood.

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

发表评论

匿名网友

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

确定