[Haskell-cafe] IORef memory leak

Don Stewart dons at galois.com
Fri Jun 19 12:09:12 EDT 2009

> 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)

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

More information about the Haskell-Cafe mailing list