er0
er0

Reputation: 1834

Debugging excess GC time in ocaml

I have written an OCaml implementation of B-trees. It is slow, and takes ~1 minute to add about 100k small ~16 byte records. When I profiled the implementation, I discovered that the program is spending most (~58 seconds) of its time in garbage collection.

The specific functions are:

   caml_garbage_collection, which consumes 58 seconds, of this:

   caml_major_collection_slice consumes 63% and
   caml_gc_dispatch consumes 23.5%

What could be the cause of this overactive garbage collection, and how would I go about debugging it?

Upvotes: 1

Views: 153

Answers (1)

er0
er0

Reputation: 1834

I ended up solving this problem by using Spacetime, a memory profiler for OCaml, and by following the instructions here: https://blog.janestreet.com/a-brief-trip-through-spacetime/

It was a very smooth experience. I identified the issue to be a debugging data structure. I was keeping a list of the entries around as a mutable reference to a list, which I was updating as follows:

t.orig_items <- new_entry :: t.orig_items

It appears that when you do this, OCaml creates a copy of the original list. So using a mutable list in this way is a bad idea, it seems.

Upvotes: 2

Related Questions