GC: Yeah, everybody "knows" how it works. How does it?

Leandro Lucarella llucax at gmail.com
Thu May 7 09:36:32 PDT 2009


Georg Wrede, el  7 de mayo a las 15:53 me escribiste:
> Frits van Bommel wrote:
> >Daniel Keep wrote:
> >>
> >>Frits van Bommel wrote:
> >>>Daniel Keep wrote:
> >>>>When your class' dtor is called, you CANNOT say whether any of the
> >>>>references into GC-controlled memory you hold are still valid.
> >>>You forgot to add: unless you know for a *fact* they're referenced from
> >>>a GC root, for example from a global variable (directly or indirectly).
> >>
> >>Or there's an integer somewhere that LOOKS like a pointer to it ...  I
> >>was talking from the perspective of NOT having any information outside
> >>of the object itself.  :P
> >No, you can't assume this one. For all you know, your program might one day be compiled with a (semi-)precise GC :).
> 
> 
> I could read the sources till I become an expert on this, but maybe it's
> more efficient, if somebody thoroughly explains to us, what is really
> going on when we have an object that has pointers to other instances,
> and it's time to collect it.
> 
> Using only two objects, adam and eve. Intially I have a reference to
> adam, and adam has a reference to eve. And what I'd of course wish, is
> that eve be destructed before adam, or at least that both's destructors
> would be run, no matter what. Let's say each has a file to close.
> 
> So, does /anybody/ know this so well that the explanation ends up being
> clear, concise, and unambiguous.

If you want to understand how the current GC works, I'd recomment reading
this series of posts:
http://proj.llucax.com.ar/blog/dgc/blog/tag/understanding%20the%20current%20gc
(from the bottom up, in chronological order, of course)

If you want to know why you don't have ordering guarantees, it's because
when the "garbage" is swept, you don't do it by following the connectivity
graph (as you do when you mark the memory). You can't do it even if you
want to, because you don't have roots to the garbage (that's why it's
garbage in the first place =). And if you can manage to follow the "old"
connectivity graph for some magical reason, you still have problems with
cycles. What if eve have a reference to adam too? What do you destroy
first? Huston, we have a problem =)

-- 
Leandro Lucarella (luca) | Blog colectivo: http://www.mazziblog.com.ar/blog/
----------------------------------------------------------------------------
GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145  104C 949E BFB6 5F5A 8D05)
----------------------------------------------------------------------------
Dentro de 30 años Argentina va a ser un gran supermercado con 15
changuitos, porque esa va a ser la cantidad de gente que va a poder
comprar algo.
	-- Sidharta Wiki



More information about the Digitalmars-d mailing list