[Haskell-cafe] foreign export space-leaks?

James Ge jge666 at gmail.com
Fri Feb 20 21:53:23 EST 2009


first I wrote a silly haskell program:

--- a program to walk through very long list ----

data Silly = A !Int | B !Int deriving (Show)

main =
  go [a + b | a <- [0..10000], b <- [0..10000]]

  where
  go [] = do
      putStrLn $ "completed."

  go (x:xs) = do
      case even x of
          True -> putStrLn $ show (A x)
          False -> putStrLn $ show (B x)
      go xs
---------- end of the program -----------

upper program ran well, and consumed fixed memory (2.8M watched by
top).
but when I exported the main to C and host the whole haskell program
in C, memory consumption increased CONSTANTLY.

what I did are:
1) explicit name the module to `Silly' and rename `main' to
`call_me_back'
2) write a c wrapper following guildlines here(http://www.haskell.org/
ghc/docs/latest/html/users_guide/ffi-ghc.html#hs-exit):

--- c wrapper code begins ----
#include <stdio.h>
#include "HsFFI.h"

#ifdef __GLASGOW_HASKELL__
#include "Silly_stub.h"
#endif

#ifdef __GLASGOW_HASKELL__
extern void __stginit_Silly ( void );
#endif

int main()
{
 hs_init(0, 0);
#ifdef __GLASGOW_HASKELL__
 hs_add_root(__stginit_Silly);
#endif

 call_me_back();

 hs_exit();
 return 0;
}

---- wrapper code ends -----

then I compiled them together:
---- Makefile section begins -----
      ghc -O2 --make Silly.hs
      ghc -O2 --make Silly_wrapper.c
      ghc -O2 -o Silly.out *.o
      ./Silly
-----Makefile sect ends -----------

this program consumed memory constantly until the process being
terminated by OS because of `out of memory'. I guess this must have
something to do with GC (although i tried import hs_perform_gc
declared in HsFFI.h and called the function, but i didn't see any
change), do you have ideas?

Thanks in advance,
jge


More information about the Haskell-Cafe mailing list