English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Optimizing garbage collection
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2010-11-19 (14:54)
From: Michael Ekstrand <michael@e...>
Subject: Re: [Caml-list] Optimizing garbage collection
On 11/18/2010 09:51 AM, Eray Ozkural wrote:
> A program I wrote constructs a lot of small lists, and strings and
> discards them. It's a search algorithm. I profiled this code and saw
> that garbage collection takes significant time.
> In C++, we can write custom allocators to optimize the data structures
> that cause such slowdowns. Any recommended strategies in ocaml?

The OCaml garbage collector exposes a variety of tuning parameters
through the Gc module[1] and the OCAMLRUNPARAM environment variable[2].
 I would tweak those.  In particular, I would recommend increasing the
minor heap size so that more of your data can be quickly discarded.  You
can also increase the space overhead, thereby causing the GC to be less
aggressive at the expense of higher memory usage/more waste.  Lastly, I
often increase the heap increment to allow memory to allow the heap to
expand more quickly, but I do not know if that will help in your case or
not.  I have documented my practices more thoroughly at my blog[3].

As I see it, the biggest gains will be by tuning your code and your
minor heap size so that ephemeral structures never hit the major heap.
My rule of thumb is that one "work unit", if you have such a concept,
should fit in the minor heap.  Collecting dead structures from the minor
heap is fast; moving a structure to the major heap only to have it be
unreachable by the next GC cycle can cause substantial GC thrashing.

You're on to a good start, though, by measuring.  I use gprof heavily as
I tweak my code's performance.

- Michael

1. http://caml.inria.fr/pub/docs/manual-ocaml/libref/Gc.html
2. http://caml.inria.fr/pub/docs/manual-ocaml/manual024.html#toc96
3. http://elehack.net/michael/blog/2010/06/ocaml-memory-tuning