[Haskell-cafe] Stack, Heap and GHC

David Roundy droundy at darcs.net
Thu Dec 14 18:22:15 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:
> 1) Which Haskell operations cost space on the stack, which cost space on
> the heap? I guess this is implementation dependent, so I looked into the
> GHC manual but did not find an answer. Where can I look these things up?

Lazily evaluated functions seem to get stuck on the stack., so space on the
stack tends to get used up by over-lazy programs, which take a long time
before they actually evaluate anything.  But I'm not quite clear myself
when exactly things go on the heap or the stack.

> 2) What could be possible sources of a stack overflow? (Apart from a
> recursive but not tail-recursive function.)

It's probably your folds.  I can never keep them straight, but quite likely
switching to a stricter variant will help you, which are named with a "'"
at the end, e.g. foldl'.  If you post your program here, I'd guess someone
will take a look at it and give you a better suggestion where the trouble
is.  It can be hard to track down, I'm afraid.

> 3) I tried using +RTS -K<size> as suggested, but these options do not
> seem to be passed through if I use --make. How can I use both, these
> compilation flags and --make?

You pass +RTS -K<size> to your executable, not when compiling (which would
affect the stack of ghc).  :)
David Roundy
Department of Physics
Oregon State University

More information about the Haskell-Cafe mailing list