[Haskell-cafe] help optimizing memory usage for a program

Austin Seipp mad.one at gmail.com
Mon Mar 2 15:51:52 EST 2009

Excerpts from Bulat Ziganshin's message of Mon Mar 02 10:14:35 -0600 2009:
> let's calculate. if at GC moment your program has allocated 100 mb of
> memory and only 50 mb was not a garbage, then memory usage will be 150
> mb

? A copying collector allocates a piece of memory (say 10mb) which is
used as the heap, and only one *half* of it ever has data in it. When
a copy occurs, all live data is transferred from one half to the other

So, if you have a 10mb heap initially, that means you have two sides
of the heap, both which are 5mb big. If your program has 2mb of live
data at the time of a GC, then those 2mb of data are copied over to
the other 5mb side. This copying can be done with pointer arithmetic
in a basic manner, so you don't need to allocate any more memory for a
copy to occur. So the program never uses more than 10mb heap. GHC
might be a little different (so correct me if I'm wrong in the case of

Copying collection has the advantage that for low-lived allocations,
GC moments are pretty quick, because the time spent copying is
proportional to the size of the live data. It gets slower if your
program has lots of live data at once, not only because collections
become more frequent, but because copies will become longer in time as


More information about the Haskell-Cafe mailing list