GC

Marc Schütz via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jul 30 02:31:33 PDT 2017


On Sunday, 30 July 2017 at 09:12:53 UTC, piotrekg2 wrote:
> I would like to learn more about GC in D. For example can 
> anyone explain why do we need memset(0) here: 
> https://github.com/dlang/phobos/blob/master/std/container/array.d#L356 , doesn't it assume a certain type of GC? What if there is a need to change the GC algorithm in the future?

The current GC is a conservative one, which means that it has at 
best an incomplete knowledge of the types in the memory it scans. 
Therefore, whenever it sees a value that, when interpreted as a 
pointer, points to a pool of memory owned by the GC, it needs to 
assume that it is indeed a pointer, which means it has to keep 
the referenced object alive.

A false pointer is one that only looks like a valid pointer but 
is in reality some other kind of data, or in this case data in a 
block of memory that's still allocated, but no longer actually in 
use. False pointers can keep GC-managed objects alive that could 
actually be freed, so they're something that should be avoided. 
The container overwrites the memory with zeroes because a zero 
value is never a valid pointer.

Changing the GC algorithm would do no harm here. The best we 
could hope for is that the GC at some pointer becomes fully 
precise (unlikely), so it can distinguish false from real 
pointers by itself. In this case, the memset would become 
unnecessary, but it wouldn't lead to wrong behaviour.


More information about the Digitalmars-d-learn mailing list