How does GC accurate work?

Go 1.3 implements an accurate garbage collector.

How to identify pointers exactly?

+3


source to share


1 answer


Looking at " Changes to the garbage collector , the mechanism seems simple:

Since Go 1.3, the runtime assumes that pointer-type values ​​contain pointers and other values .

This assumption is fundamental to the exact behavior of both stack expansion and garbage collection.

Programs using an unsafe package to store integers in pointer-entered values ​​are illegal and will crash if the runtime detects behavior.
Programs that use an unsafe package to store pointers to integer values ​​are also illegal, but more difficult to diagnose at runtime.



This reddit thread adds:

Basically the GC has to figure out which objects are reachable, for that it has to follow the pointers on the stack to every object they point to, and then follow the pointers in the objects to every object they point to until it encounters more new objects.
Every object that is not encountered by the GC is garbage.

The problem is that it requires the GC to know what a pointer is:

  • the exact GC has this information,
  • The conservative GC should assume that every value on the stack can be a pointer if it is identical to the address of the allocated object.

As a result, conservative GCs tend to contain a lot of unreachable objects and have to do more work (tracing dead object graphs).

+5


source







All Articles