[Haskell-cafe] puzzling memory leak? (GHC)

David Roundy droundy at abridgegame.org
Tue Oct 11 09:41:21 EDT 2005

Hi Young,

Just to give a somewhat different perspective, sometimes increasing
laziness is helpful for avoiding space problems--not really so much space
"leaks" as space wastage.

In this case, you probably can't afford to hold "contents" in memory at any
given time.  So you need to be certain both that as it is consumed it is no
longer needed (which is reflected in the strictness suggestions given by
others), but you also (most likely) don't want to hold the entire list of
Arts in memory either, since that'll also take a huge amount of memory.
That requires that parseArts generates its output lazily and that fixArts
consume it properly.  Both of those appear to be the case from the code you
outlined, but I figured I'd point out that strictness in the wrong
circumstances can be as bad as laziness for your memory usage.

Assuming the following is correct...

> parseArts (x:xs) ... = (createArts x) : parseArts xs

then it would be worth checking that 

> main = do
>        contents <- getContents
>        fixArts [head $ parseArts contents [] (Map.singleton "offset" 0)]

takes very little memory.  If this takes a large amount of memory, then I
think you may have a problem with parseArts being insufficiently lazy.
David Roundy

More information about the Haskell-Cafe mailing list