[Haskell-cafe] Stack, Heap and GHC

David Roundy droundy at darcs.net
Thu Dec 14 18:31:54 EST 2006

On Thu, Dec 14, 2006 at 04:00:53PM +0100, Felix Breuer wrote:
> Hello everyone,
> I have been trying to run a Haskell program of mine that does an
> extensive computation with very large amounts of data. I compiled the
> program with ghc --make. When I run it it terminates after some time
> with the message:
>   Stack space overflow: current size 8388608 bytes.
>   Use `+RTS -Ksize' to increase it.
> The program isn't that well written so the overflow did not surprise me,
> I expected that it might run out of memory. What did surprise me was the
> *stack* overflow. I do not use recursion in my program except for a
> couple of fold operations over very large lists. So I have a number of
> questions:

Here's a little program that can illustrate this issue:

import Data.List

largenum = 1000000

main = do putStrLn "strict foldl1"
          print $ foldl1' (\a b -> a + 1) $ [1..largenum]
          putStrLn "lazy foldl1"
          print $ foldl1 (\a b -> a + 1) $ [1..largenum]

It gets through the first one, but not the second call, which differs only
in the strictness of the foldl.  You can make it use up more memory by
making largenum a hundred times bigger, in which case for some reason it
doesn't seem to have a stack error (although it hasn't completed on my
computer, and uses something like 2G of memory).  Perhaps the thunks are
placed on the heap, and only when they are actually evaluated does anything
go onto the stack?
David Roundy
Department of Physics
Oregon State University

More information about the Haskell-Cafe mailing list