What to do when garbage collector is slow?

Adam Gundry adam at well-typed.com
Mon Jan 5 16:44:31 UTC 2015

Hi David,

On 23/12/14 10:59, David Spies wrote:
> I have a program that, to all appearances, is behaving properly.  It
> uses very little memory to run, it has the profile I would expect
> looking at +RTS -hc.  I have no reason to believe there is a memory leak
> (in the sense that it's not lazily holding on to things it no longer
> needs or strictly generating things it doesn't need yet).  But it's
> slow, and according to -sstderr, most of the time is spent
> garbage-collecting.
> Why is the garbage-collector consuming so much running time? How can I
> deal with it?

It's entirely possible that your program is allocating a lot of data
that quickly becomes garbage, even if it is not holding on to it for too
long (i.e. leaking memory). Remember that the heap profile shows only
data that is being retained, so there might be unnecessary allocation
even if the heap profile looks sensible. You can use a time and
allocation profile (-p) to find out what is doing most of the allocation.

A quick glance at the profile suggests that funArray and mapWithIdx,
both of which construct lists and then immediately turn them into
arrays, are the likely culprits. Perhaps you can avoid constructing the
intermediate lists?

Hope this helps,


> The program is a solution to this problem:
> https://open.kattis.com/problems/tourist
> The input data can be found here:
> http://heim.ifi.uio.no/~db/nm-i-programmering/nm2004/testdata/h.in

Adam Gundry, Haskell Consultant
Well-Typed LLP, http://www.well-typed.com/

More information about the Glasgow-haskell-users mailing list