duange2971 2015-02-28 02:52
浏览 18
已采纳

谁进行垃圾收集工作?

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条回答 默认 最新

  • doueta6642 2015-02-28 03:11
    关注

    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.

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

报告相同问题?

悬赏问题

  • ¥15 python点云生成mesh精度不够怎么办
  • ¥15 QT C++ 鼠标键盘通信
  • ¥15 改进Yolov8时添加的注意力模块在task.py里检测不到
  • ¥50 高维数据处理方法求指导
  • ¥100 数字取证课程 关于FAT文件系统的操作
  • ¥15 如何使用js实现打印时每页设置统一的标题
  • ¥15 安装TIA PortalV15.1报错
  • ¥15 能把水桶搬到饮水机的机械设计
  • ¥15 Android Studio中如何把H5逻辑放在Assets 文件夹中以实现将h5代码打包为apk
  • ¥15 使用小程序wx.createWebAudioContext()开发节拍器