[Haskell-cafe] IORef memory leak
Jim Snow
jsnow at cs.pdx.edu
Fri Jun 19 14:13:59 EDT 2009
Don Stewart wrote:
> dvde:
>
>> Don Stewart schrieb:
>>
>>> It is not possible to write a modifyIORef that *doesn't* leak memory!
>>>
>>>
>> Why? Or can one read about it somewhere?
>>
>
>
> Try writing a version of this program, using modifyIORef only,
> such that it doesn't exhaust the heap:
>
> import Data.IORef
> import Control.Monad
> import System.IO.Unsafe
>
> ref :: IORef Int
> ref = unsafePerformIO $ newIORef 0
> {-# NOINLINE ref #-}
>
> main = do
> modifyIORef ref (\a -> a + 1)
> main
>
> Run it in a constrained environment, so you don't thrash:
>
> $ ./A +RTS -M100M
> Heap exhausted;
> Current maximum heap size is 99999744 bytes (95 MB);
> use `+RTS -M<size>' to increase it.
>
> The goal is to run in constant space.
>
> -- Don
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
Thanks, that's good to know.
do x <- readIORef ior
writeIORef ior $! (x+1)
Works for me. The laziness of modifyIORef and workarounds would be a
good thing to have documented in the modifyIORef docs [1], since it's
probably a common source of memory leaks. I'd also be in favor of a
strict version of modifyIORef.
[1]
http://www.haskell.org/ghc/dist/current/docs/libraries/base/Data-IORef.html#v%3AmodifyIORef
-jim
More information about the Haskell-Cafe
mailing list