Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Which function is consing?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2007-07-04 (15:57)
From: Brian Hurt <bhurt@s...>
Subject: Re: [Caml-list] Which function is consing?

On Wed, 4 Jul 2007, Joel Reymont wrote:

> What I would like to find out is how much memory each function allocates. I 
> would like to minimize memory allocations as they put pressure on the garbage 
> collector.

Are you trying to track down a specific performance problem, or just 
trying to avoid allocation in general?

If it's the former, I've had good luck just hand analyzing functions. 
Once you know the boxing rules, each block allocated in memory has one tag 
word associated with it.  So if I see "x :: lst" in my code, I know that 
just allocated 3 words- a tag word for the list block, plus a value and a 
next pointer.  And so on.  One of the nice things about Ocaml vr.s Haskell 
is that this sort of analysis is much easier to do in Ocaml.

If you're just trying to reduce memory allocation in general, my advice is 
to not bother, unless you have evidence that 1) performance isn't 
acceptable, 2) higher level optimizations (for example, replacing O(N^2) 
operations with O(N log N) operations) either cannot be applied or would 
be ineffective, and 3) garbage collection costs from excessive allocation 
is the problem.

One comment I will make: it's not unusual for C/C++ programs to spend 
considerable amount of time allocating and deallocating objects as well, 

And note that this study does not include any overhead of deciding if an 
object is freeable, which Ocaml's GC does as well.  C/C++ allocate and 
deallocate a heck of a lot less, but the cost of allocating and 
deallocating is a heck of a lot higher.  The difference is that in Ocaml, 
all of these costs are reported as one big number- "hey, you're spending 
30% of your time in GC!"  While if you were spending 30% of your time in 
destructors deciding which objects can be freed yet or not, since that 
time is spread over a bunch of different functions, it's much less