Thank you, for your answer.
Sven Panne wrote:
> They are run when the RTS recognizes that the object is dead, which
> is not necessarily at the first GC after the object is unreachable
> (GHC uses a generational garbage collector by default).
Is their a possibility to ensure that they will run, before the program
terminates.
I have allocated objects which eat up more then 20MB in ghc and the
finalizers won't run.
In deed I have never seen a finalizer running before termination.
>
>
> It is only guaranteed that they are *eventually* run.
>
>
Eventually. I have to kill some threads, holding other Objects and to
synchronise with an external database when the object is dead.
> > If I trigger a gc in hugs the object is garbage collected but not
> > finalized.
>
> Which version of GHC/Hugs are you using exactly?
>
GHC: 4.06
Hugs: I don't know how to identify this. It just says Hugs 98.
It's the version of Suse Linux 6.4 from May 13th.
> > In ghc I have to use finalize, but that does not run the finalizer.
>
> If you are talking about Weak.finalize this would indeed be a bug.
> Do you have a small example? Are you using weak pointers and/or
> foreign objects?
>
I'm talking about Weak.finalize and using weak-pointers for checking the
status, if the object is collected.
I don't use foreign objects. I would like to kill threads which hold
subobjects with the aid of finalizers.
I will attach a short example.
I used these options for running: +RTS -B -G1 -A5k -RTS
I hope you will apologize my eventually bad or unusual programming
style. I
just learned Haskell for my thesis.
Thanks in advance
uno
--
comp.: Linux IT-Management
addr.: Ulrich Norbisrath; Weissdornweg 40; 52223 Stolberg
mbl.: +49 179 5164025 tel.: +49 2402 936146 fax.: +49 2402 37343
mailto:[EMAIL PROTECTED] www: http://www.linuxloesung.de
-- based on an example of hugs
import Weak
import Concurrent
-- primitive gc "primGC" :: IO () -- uncomment this for use with hugs and comment
-- the later
gc = do
return ()
main = test 4
-- not a CAF!
test z =
do
let k = [z]
let port = ([z],k) -- use a list so we're sure it's heap allocated
testvar <- newMVar False
wert <- readMVar testvar
putStr "1 - mvar: "
print wert
putStr "2 - k: "
print k -- this makes sure x is in whnf
w <- mkWeak port "test"
(Just (do
wert <- takeMVar testvar
putStr "3- finalize mvar: "
print wert
putMVar testvar True
putStrLn ("4 - Finalizer for "++show port)
))
-- w <- mkWeak port "test" (Just (putStrLn ("Finalizer for "++show k)))
-- w <- mkWeakPtr "test" (Just (putStrLn ("Finalizer for "++show k)))
-- note that the finalizer uses the key, but
-- this shouldn't keep the weak ptr alive!
showWeakPtr w
putStrLn "5 - Doing gc."
gc
putStr "6 - port: "
print port -- ensure port is not collected up to this point
showWeakPtr w
putStrLn "7 - Doing gc."
gc
putStr "8 - k: "
print k
showWeakPtr w
-- finalize w -- when not uncommented, no finalizition and
-- collecting of port (in ghc)will ever run
-- in hugs, port will be collected, but no finalization
wert <- readMVar testvar
putStr "9 - mvar: "
print wert
newgrow <- newMVar 12
doshow w k 12 [newgrow] -- wait for gc and finilazing, eat memory meanwhile
putStrLn "11 - Bye!" -- will never run
doshow w k count grow = -- wait for gc and finalizing
do
putStrLn ((show count) ++ " - Sleeping")
threadDelay (1000000) -- sleep, for fast eating up heap, uncomment
putStr "Weak Pointer: "
showWeakPtr w
putStr "k: "
print k
newgrow <- newMVar count
wert <- readMVar newgrow
putStr "newgrow MVarVal: "
print wert
doshow w k (count+1) (grow ++ [newgrow])
showWeakPtr :: Show a => Weak a -> IO ()
showWeakPtr w =
do
x <- deRefWeak w
putStr "showWeakPtr: "
print x