Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Go language garbage collection approach compared to others?

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.

like image 544
Saeed Zarinfam Avatar asked Jan 14 '13 16:01

Saeed Zarinfam


1 Answers

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.

like image 52
Russ Cox Avatar answered Sep 16 '22 15:09

Russ Cox