I have a space leak in my Haskell programm, I could pinpoint it to a minimal example as follows. I would expect the following code (albeit not terminating but I don't care) to run in constant memory but it doesn't:
head . filter (const False) $ (transpose [[1..]])
while head . filter (const False) $ [1..]
does run in constant memory.
So it must have to do with usage of 'transpose' on infinite lists I guess.
Ignoring the more complicated base library implementation, if we define transpose like transpose xss = map head xss : transpose (map tail xss)
, why is there a space leak ? My assumption was that the garbage collector could free the memory formerly consumed by map head xss
in every step of the filter
function. I guess that the map tail xss
somehow prevents that ?! anyway, could I add some sort of strictness annotation or similar to transpose
so that that simple example does run in constant memory?
main = print . <code in question>
with-O2 -rtsopts
and running with+RTS -M1g
to set the maximum heap size to 1 gig (which surely ought to be enough if the GC can collect something) results in the process dying from allocating too much. – Maidel