[Haskell-cafe] Memory

Don Stewart dons at galois.com
Tue Feb 17 00:03:49 EST 2009


inbuninbu:
> Hello All,
> 
> The kind people at #haskell suggested I come to haskell-cafe for
> questions about haskell performance issues.
> I'm new to haskell, and I'm having a hard time understanding how to
> deal with memory leaks.
> 
> I've been playing with some network server examples and I noticed with
> each new connection, the memory footprint increases by about 7k
> However, the leaks don't seem to have anything to do with the
> networking code. Actually I get a huge leak just from using using
> 'forever'.
> 
> > import Control.Monad
> > import System.IO
> >
> > main = forever $ putStrLn "hi"
> 
> When I run it for a few seconds with profiling...
> 
> > total time  =        0.36 secs   (18 ticks @ 20 ms)
> > total alloc =  54,423,396 bytes  (excludes profiling overheads)
> 
> Can this be right?


did you compile with optimisations on?

$ ghc -O2 A.hs --make
$ time ./A +RTS -sstderr

          17,880 bytes maximum residency (1 sample(s))
          18,984 bytes maximum slop
               1 MB total memory in use (0 MB lost due to fragmentation)

  Generation 0:  9951 collections,     0 parallel,  0.07s,  0.07s elapsed
  Generation 1:     1 collections,     0 parallel,  0.00s,  0.00s elapsed

  INIT  time    0.00s  (  0.00s elapsed)
  MUT   time    4.45s  ( 16.08s elapsed)
  GC    time    0.07s  (  0.07s elapsed)
  EXIT  time    0.00s  (  0.00s elapsed)
  Total time    4.52s  ( 16.16s elapsed)

  %GC time       1.5%  (0.5% elapsed)

  Alloc rate    1,173,414,505 bytes per MUT second

  Productivity  98.5% of total user, 27.5% of total elapsed

./A +RTS -sstderr  4.52s user 10.61s system 93% cpu 16.161 total

So it's allocating small cells, frequently, then discarding them -- and running 
in constant space.

Looking further,

    forever     :: (Monad m) => m a -> m b
    forever a   = a >> forever a

Well, here, the result is thrown away anyway. And the result is (), so I'd expect 
constant space. 

Looks good to me. Did you run it without optimisations, on , perhaps?


-- Don


More information about the Haskell-Cafe mailing list