英文:
What is the Go language garbage collection approach compared to others?
问题
我对Go编程语言了解不多,但我看到有人声称Go具有无延迟垃圾回收,并且比其他垃圾回收器(如JVM垃圾回收器)要好得多。我开发过JVM应用程序,我知道JVM垃圾回收器不是无延迟的(尤其是在大内存使用情况下)。
我想知道,Go中的垃圾回收方法与其他方法有何不同,使其成为无延迟的?
提前感谢。
英文:
I do not know much about the Go programming language, but I have seen several claims that said Go has latency-free garbage collection, and it is much better than other garbage collectors (like JVM garbage collector). I have developed application for JVM and i know that JVM garbage collector is not latency-free (specially in large memory usage).
I was wondering, what is difference between the garbage collection approach in Go and and the others which make it latency-free?
Thanks in advance.
Edit:
@All I edited this question entirely, please vote to reopen this question if you find it constructive.
答案1
得分: 21
Go没有无延迟垃圾回收。如果你能指出这些说法在哪里,我想尝试纠正它们。
我们认为Go相比Java的一个优势是它可以更好地控制内存布局。例如,一个简单的2D图形包可能定义如下:
type Rect struct {
Min Point
Max Point
}
type Point struct {
X int
Y int
}
在Go中,一个Rect只是内存中连续的四个整数。你仍然可以将&r.Max传递给期望*Point的函数,那只是指向Rect变量r中间的指针。
在Java中,等效的表达式将是创建Rect和Point类,这样Rect中的Min和Max字段将是指向单独分配的对象的指针。这需要更多的分配对象,占用更多的内存,并给垃圾回收器带来更多的跟踪和工作。另一方面,它确实避免了需要创建指向对象中间的指针。
与Java相比,Go给程序员更多地控制内存布局,并且您可以使用该控制来减少垃圾回收的负担。这在具有大量数据的程序中非常重要。对于从硬件中提取性能来说,对内存布局的控制也可能很重要,因为缓存效果等原因,但这与原始问题无关。
当前Go发行版中的垃圾回收器是合理的,但绝不是最先进的。我们计划在未来一两年内投入更多的精力来改进它。明确地说,
Go的垃圾回收器当然不如现代Java垃圾回收器好,但我们相信在Go中编写不需要太多垃圾回收的程序更容易,因此总体效果仍然是Go程序中垃圾回收问题比等效的Java程序少。
英文:
Go does not have latency-free garbage collection. If you can point out where those claims are, I'd like to try to correct them.
One advantage that we believe Go has over Java is that it gives you more control over memory layout. For example, a simple 2D graphics package might define:
type Rect struct {
Min Point
Max Point
}
type Point struct {
X int
Y int
}
In Go, a Rect is just four integers contiguous in memory. You can still pass &r.Max to function expecting a *Point, that's just a pointer into the middle of the Rect variable r.
In Java, the equivalent expression would be to make Rect and Point classes, in which case the Min and Max fields in Rect would be pointers to separately allocated objects. This requires more allocated objects, taking up more memory, and giving the garbage collector more to track and more to do. On the other hand, it does avoid ever needing to create a pointer to the middle of an object.
Compared to Java, then, Go gives you the programmer more control over memory layout, and you can use that control to reduce the load on the garbage collector. That can be very important in programs with large amounts of data. Control over memory layout may also be important for extracting performance from the hardware due to cache effects and such, but that's tangential to the original question.
The collector in the current Go distributions is reasonable but by no means state of the art. We have plans to spend more effort improving it over the next year or two. To be clear,
Go's garbage collector is certainly not as good as modern Java garbage collectors, but we believe it is easier in Go to write programs that don't need as much garbage collection to begin with, so the net effect can still be that garbage collection is less of an issue in a Go program than in an equivalent Java program.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论