Why no stack overflow in ghci?

Simon Marlow marlowsd at gmail.com
Mon Aug 1 16:12:47 CEST 2011

On 26/07/2011 22:41, Albert Y. C. Lai wrote:
> On 11-07-26 03:57 PM, Roman Cheplyaka wrote:
>> The code
>> main = print $ foldl (+) 0 [1..1000000]
>> when compiled (without optimizations) results in a stack overflow, which
>> is expected.
>> However, when run from ghci it succeeds. Why is it so?
>> (Tested with GHC 6.12.something and 7.0.4)
> To enjoy unlimited stack in compiled code, add +RTS -K10
> To enjoy limited stack in ghci, add +RTS -K8M
> To know why, see my
> http://www.mail-archive.com/haskell-cafe@haskell.org/msg88368.html

The message linked here doesn't really explain *why*, rather it seems to 
be a bug report on haskell-cafe (which I don't usually read, so it 
doesn't get fixed!).

GHC itself, and hence also GHCi, have a default maximum stack size of 
512M, whereas the default for compiled programs is 8M.  I think this is 
probably way too low, but the reason there is a maximum at all is 
because it is very easy to accidentally write a program with an infinite 
loop and some OSs cope rather badly when a program eats all the memory.

I have no idea what was going on with the magic -K10 option, but it 
seems to be fixed in 7.2.1:

$ ghc +RTS -K1
ghc-stage2: error in RTS option -K1: size outside allowed range (8 - 
$ ghc +RTS -K8
stack overflow: use +RTS -K<size> to increase it
$ ghc +RTS -K60
stack overflow: use +RTS -K<size> to increase it
$ ghc +RTS -K80
stack overflow: use +RTS -K<size> to increase it


More information about the Glasgow-haskell-users mailing list