garbage collection for a data structure

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

On Jan 19, 2015 7:10 AM, "Elise Huard"
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.

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! Ah, thanks, that clears it up somewhat
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. Ah, you mean the literals may fall under this? https://ghc.haskell.org/trac/ghc/wiki/Commentary/Rts/Storage/GC/CAFs?redirec... Or are they optimized away because never actually used? Thanks, I learned something. I changed the program to perform an operation and print out the result with the values, but the finalizer still didn't run - I might shelve this one for later examination. Thank you,
Elise

Elise,
I remember that your earlier email on the beginner list asked about
"diffing from moment to moment." Perhaps you can sketch what you're trying
to achieve?
Because once you get into the internals of GC the learning ramp gets very,
very steep. There are traps known and unknown for both the wary and unwary,
as a cursory overview of GHC trac will inform.
There's probably a way of getting things to work without relying on
implementation-specific haskell.
-- Kim-Ee
On Mon, Jan 19, 2015 at 7:10 PM, Elise Huard
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 _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (3)
-
David Feuer
-
Elise Huard
-
Kim-Ee Yeoh