Are heap objects never moved by the garbage collector?

Diggory diggsey at googlemail.com
Sat Jun 1 03:42:03 PDT 2013


On Saturday, 1 June 2013 at 08:11:05 UTC, sclytrack wrote:
> On Friday, 31 May 2013 at 16:31:39 UTC, Carl Sturtivant wrote:
>>
>> "The D Programming Language" (TDPL) p.178 asserts the 
>> following.
>>
>> "The objects themselves stay put, that is their locations in 
>> memory never change after creation."
>>
>> I take this to mean that the D garbage collector doesn't move 
>> live objects and adjust all references to them the way that 
>> some garbage collectors do. That is to say, the addresses of 
>> objects are not changed by the garbage collector.
>>
>> Does D guarantee this?
>
> No. Quoted from the D website:
>
> http://dlang.org/garbage.html
>
> "Although D does not currently use a moving garbage collector, 
> by following the rules listed above one can be implemented. No 
> special action is required to pin objects. A moving collector 
> will only move objects for which there are no ambiguous 
> references, and for which it can update those references. All 
> other objects will be automatically pinned. "
>
> I'll assume you can not move immutable pointers. Immutable 
> pointers are not mentioned on that page. (except in the index 
> left :)

Actually that's wrong - a moving GC would break everything in D 
since the GC cannot know if there are any ambiguous references. 
As soon as you pass a pointer to an external function there's no 
way for the GC to know if that pointer or any pointers accessible 
from it will ever be safe to move again. Pinning has to be 
explicit or it doesn't work, and currently all of phobos and 
druntime is implemented without it.


More information about the Digitalmars-d-learn mailing list