[Haskell-cafe] trying to understand out of memory exceptions

Anatoly Yakovenko aeyakovenko at gmail.com
Tue Apr 16 20:03:43 CEST 2013


-- So why does this code run out of memory?

import Control.DeepSeq
import System.IO
import qualified Data.ByteString.Char8 as BS

scanl' :: NFData a => (a -> b -> a) -> a -> [b] -> [a]
scanl' f q ls =  q : (case ls of
                        []   -> []
                        x:xs -> let q' = f q x
                                in q' `deepseq` scanl' f q' xs)


main = do
   file <- openBinaryFile "/dev/zero" ReadMode
   chars <- BS.hGetContents file
   let rv = drop 100000000000 $ scanl' (+) 0 $ map fromEnum $ BS.unpack
chars
   print (head rv)

-- my scanl' implementation seems to do the right thing, because

main = print $ last $ scanl' (+) (0::Int) [0..]

-- runs without blowing up.  so am i creating a some thunk here?  or is
hGetContents storing values?  any way to get the exception handler to print
a trace of what caused the allocation?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20130416/5744c10c/attachment.htm>


More information about the Haskell-Cafe mailing list