Does the GC prioritize same-class when looking for things to free?

Steven Schveighoffer schveiguy at gmail.com
Thu Sep 15 17:26:30 UTC 2022


On 9/15/22 1:12 PM, cc wrote:
> Why is Foo never deallocated here? (`DMD32 D Compiler v2.099.0-dirty` 
> win64)

In answer to your title question, no. It does not prioritize anything. 
If it thinks something is ready to be freed, it is freed. If it thinks 
something is not ready to be freed, it is not freed.

So why is it not deallocated? There is no requirement for a specific 
piece of garbage to be collected at any specific time. Maybe something 
is referring to it on a register, and that register is still present? 
Maybe it's stored it somewhere on the stack? Maybe there's a false 
positive from a stack variable that isn't a pointer? For whatever 
reason, the GC still thinks it's alive.

People pull their hair out a lot trying to explain why the GC does or 
does not free something. Sometimes it just works one way on some 
environments, and differently on others.

I'd recommend just forgetting trying to prove whether the GC works or 
not, or that it works a specific way. Unless you are having a specific 
problem with memory leaks or things that are being freed that shouldn't 
be, don't worry about it.

-Steve


More information about the Digitalmars-d-learn mailing list