current GC behavior

thedeemon dlang at thedeemon.com
Tue Nov 6 09:02:00 PST 2012


On Tuesday, 6 November 2012 at 11:27:25 UTC, luka8088 wrote:
> Hello everyone,
>
> 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.

Those are some sample cases which all fit into the general rule: 
on collection the stack is scanned and everything that looks like 
a pointer to live heap is used to mark heap objects as live, then 
the heap objects are scanned and marked transitively. Unmarked 
objects get collected. In each case described in the original 
post the decision whether some value will be collected or not 
depends on whether the local variable is still on the stack and 
not overwritten by some newer stack variable. As a consequence:
If it's going to be used later, it's still live on stack.
If it's not going to be used later, it all depends on whether 
some other variable defined later is placed by compiler in the 
same stack slot, and whether that new variable was initialized 
already.
If it was used in a function called and returned, it's not in the 
active stack, so it should be collected. And so on.


More information about the Digitalmars-d-learn mailing list