在垃圾回收语言中实现的虚拟机的垃圾回收机制

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

garbage-collection for VM implemented in a garbage-collected language

问题

我的问题是关于垃圾回收的一般性问题,但我将以Python作为例子。但首先我需要用一些事实或者至少我认为是事实的东西来构建我的问题,欢迎纠正我。

Python的CPython实现是用C编写的虚拟机,其中包含一个垃圾回收器。但是Jython是在JVM上运行的Python实现,它不需要实现一个垃圾回收器,因为它只是将Python编译为JVM将要运行的Java字节码,而JVM已经有了一个垃圾回收器。

但是假设我想使用Golang实现一个Python虚拟机。据我所知,当你编译Go代码时,Go编译器会将一个运行时附加到你的编译二进制文件中,其中包含一个垃圾回收器,所以当我编译我的虚拟机时,它将包含Go运行时和垃圾回收器。这是否意味着如果我用Golang编写一个Python虚拟机,我就不需要实现一个垃圾回收器呢?

谢谢。

英文:

My question is about garbage-collection in general but I'm going to use python as an example. But I need to build up my question first with some facts, or at least what I think are facts, feel free to correct me.

The CPython implementation of python is a virtual machine written in C which contains a garbage-collector. But Jython, which is an implementation of python that runs on JVM, does not need to implement a garbage-collector, because all it's doing is compiling python to java bytecode that the JVM is going to run and JVM already has a garbage-collector.

But suppose I want to implement a python VM using Golang. As far as I know, when you compile your Go code, the Go compiler attaches a runtime to your compiled binary which contains a garbage-collector, so when I compile my VM, it's going to contain the Go runtime and garbage-collector. Does that mean that if I write a python VM in Golang, I won't need to implement a garbage-collector?

Thanks

答案1

得分: 2

Go的垃圾回收器将了解您的虚拟机的结构,而不是虚拟机本身处理的代码。

所以答案是:您仍然需要为您的虚拟机实现一个垃圾回收器。

例如:想象一个非常简单的虚拟机,它有一些内存和一些指令。内存可以是Go中的字节切片。但是这将是一个切片,从开始到结束都在使用。只要虚拟机在使用,垃圾回收器就不会清除并将这块内存返回给操作系统。

但是,如果您可以将虚拟机的某些方面映射到Go中,也许您可以获得一些优势。例如,您可以将一个线程映射到一个goroutine。但是垃圾回收器需要额外的工作。您需要知道是否有对该内存部分的引用,以便清理它。这完全取决于虚拟机的具体情况。

英文:

The go garbage collector will understand the structures of your VM, not about the code that the VM itself will handle.

So the answer is: you still need to implement a garbage collector for your VM

For instance: imagine a very simple Virtual Machine with some memory and few instructions. The memory can be a slice of bytes in Go. But will ne one slice, used from begin to end. The GC will not clear and return this piece of memory to the OS while the VM is using.

But if you can map some aspects of this VM to Go, perhaps you may have some advantage. For instance, you may map a thread to a goroutine. But a GC need some extra effort. You need to know that there is no references to that portion of memory to clean it. And this is totally specific to the VM

答案2

得分: 2

答案取决于你如何实现Python虚拟机。

通常,解释器需要跟踪程序中声明的所有符号。因此,即使程序删除了对对象的所有引用,运行时(Go程序)仍会保留对它的引用,防止其被垃圾回收。这样的实现将需要一个自定义的垃圾回收器。

如果你的解释器对每个符号进行引用计数,并在所有对它的引用都消失后立即释放它,就可以避免使用自定义垃圾回收器。在这种情况下,Go的垃圾回收器最终会回收它。引用计数通常更容易实现,但对于长时间运行的程序可能效率较低。

英文:

The answer depends on how you implement the Python VM.

Usually, an interpreter will need to keep track of all the symbols declared in a program. Thus even if the program removes all the references to an object, the runtime (Go program) will keep a reference to it, preventing it from being garbage collected. Such an implementation will require a custom garbage collector.

A custom garbage collector can be avoided if your interpreter does reference counting for every symbol, and releases it as soon as all the references to it are gone. In that case, the Go garbage collector will collect it eventually. Reference counting is usually easier to implement, but may be less efficient for long-running programs.

huangapple
  • 本文由 发表于 2022年3月28日 00:49:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/71638331.html
匿名

发表评论

匿名网友

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

确定