current GC behavior
luka8088
luka8088 at owave.net
Tue Nov 6 12:00:07 PST 2012
On 6.11.2012 18:00, Ali Çehreli wrote:
> On 11/06/2012 03:27 AM, luka8088 wrote:
>
> > I was writing some unit tests and I also wanted to test that in certain
> > cases object references are properly removed everywhere so that GC can
> > collect them in order to make sure there is no memory leak. While trying
> > to achieve this I learned that objects are not always collected at the
> > time I would expect them to, so I documented current behavior and some
> > tips.
>
> Thanks for the analysis and the tips but they all depend on observations
> made by a particular test program on a particular version of a
> particular compiler. It is conceivable that even the same GC algorithm
> can behave differently in another program.
Yes, but it seems that we can in general say that the following code
will never fail... or am I wrong ?
import core.memory;
class a {
static int totalRefCount = 0;
this () { totalRefCount++; }
~this () { totalRefCount--; }
}
void main () {
assert(a.totalRefCount == 0);
({
auto a1 = new a();
assert(a.totalRefCount == 1);
})();
GC.collect();
assert(a.totalRefCount == 0);
}
>
> In fact, some objects may never be collected even if there are no more
> references to them.
Can you give me an example ?
>
> In comparison, destroy() and scoped() do call the destructors at precise
> times.
Yes, but the main reason for doing this is testing for memory leaks so
this cases are not much of a concern =)
>
> Ali
>
Luka
More information about the Digitalmars-d-learn
mailing list