[Haskell-cafe] garbage collection for a data structure

David Feuer david.feuer at gmail.com
Mon Jan 19 16:35:55 UTC 2015

On Jan 19, 2015 7:10 AM, "Elise Huard" <haskell at elisehuard.be> wrote:
> Hi,
> I was wondering if there was a way to check whether a particular data
> structure gets garbage collected in a program.  A friendly person
> pointed me to System.Mem.Weak on the Haskell-Beginner list - however
> I've been unable to verify how it works, so I'm bumping it to this
> list.

Yes, sort of. You can use that to tell if a certain *constructor* has been
collected, but that's not always so helpful. For instance, if you make a
weak pointer to a list, and find that it's been collected, that just means
the first (:) constructor has been collected. It may well be that the rest
of the list spine and all its elements are still live. I ran into this
problem trying to find out if an Array had been collected. It turned out
that the Array constructor that holds the array bounds had been, but the
actual Array# holding the elements was still around!

> See the following toy program: I was trying to see whether the output
> would contain "garbage collected".

I'm not sure what the deal is there exactly, but my *guess* is that GHC may
never actually bother allocating x or z at all. Finalizers are not
guaranteed to ever run; they're a sort of "please, if you have the time"
kind of thing.

> I wondered if performGC is a nudge rather than an immediate "garbage
> collect now" instruction, and performGC is not actually performed?

No, I think that's a pretty definite "Do this now." I just think the
garbage you *think* exists and the garbage that *actually* exists are
probably different.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20150119/4c1374dc/attachment.html>

More information about the Haskell-Cafe mailing list