[Haskell-cafe] how out of memory is handled in Haskell

Don Stewart dons at galois.com
Tue Jan 6 18:32:02 EST 2009


manlio_perillo:
> Hi.
> 
> Here:
> http://damienkatz.net/2008/03/what_sucks_abou.html
> 
> I found how Erlang (or at least old versions of Erlang) handles out of
> memory failure: it just calls exit(1).
> 
> 
> How is this handled in GHC?
> - exit(1)?
> - abort()?
> - IO exception?
> 
> 

GHC:

    $ ./A +RTS -M2M
    Heap exhausted;
    Current maximum heap size is 1998848 bytes (1 MB);
    use `+RTS -M<size>' to increase it

Which is invoked via the OutOfHeapHook function:

    #include "Rts.h"
    #include <stdio.h>

    void
    OutOfHeapHook (lnat request_size, lnat heap_size) /* both sizes in bytes */
    {
      /*    fprintf(stderr, "Heap exhausted;\nwhile trying to allocate %lu bytes in a %lu-byte heap;\nuse `+RTS -H<size>' to increase the total heap size.\n", */

      (void)request_size;   /* keep gcc -Wall happy */
      fprintf(stderr, "Heap exhausted;\nCurrent maximum heap size is %lu bytes (%lu MB);\nuse `+RTS -M<size>' to increase it.\n",
          heap_size, heap_size / (1024*1024));
    }

Which you can modify yourself at link time.

It is invoked as:

    void
    heapOverflow(void)
    {
      OutOfHeapHook(0/*unknown request size*/, 
            RtsFlags.GcFlags.maxHeapSize * BLOCK_SIZE);
      
      stg_exit(EXIT_HEAPOVERFLOW);
    }

That is, your OutOfHeapHook is called, and then stg_exit is called to shut down the runtime with:

    void  
    stg_exit(int n)
    { 
    #ifdef PAR
      if (exit_started) 
        return;
      exit_started=rtsTrue;

      shutdownParallelSystem(n);
    #endif

      if (exitFn)
        (*exitFn)(n);
      exit(n);
    }

Where you can also set your own exitFn, before, if all else fails, exit() is called.

-- Don


More information about the Haskell-Cafe mailing list