[Haskell-cafe] garbage collection for a data structure

Elise Huard haskell at elisehuard.be
Mon Jan 19 12:10:38 UTC 2015


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.

See the following toy program: I was trying to see whether the output
would contain "garbage collected".
I wondered if performGC is a nudge rather than an immediate "garbage
collect now" instruction, and performGC is not actually performed?  Or I've
misunderstood finalizers in this context and they would not actually
be executed when z gets garbage collected?

import System.Mem.Weak
import System.Mem (performGC)
import Control.Concurrent (threadDelay)


main :: IO ()
main = do let x = 5
              y = "done"
              z = 3
          a <- mkWeak z x (Just (putStrLn "garbage collected"))
          performGC
          threadDelay 20000000
          print y

Thank you,

Elise


More information about the Haskell-Cafe mailing list