How to run an OCaml program with GC disabled?

For some benchmark, I would like to disable GC.
As in “don’t garbage collect unless we are out of RAM”.
I guess it is only the major collection that should be forbidden.

Is this possible to run an OCaml program like this?

Why is that? Minor collections also stop the program.

Note that the GC pauses in OCaml are very brief, including for major collections. (Only parts of the major heap are collected at a time.)

I have never tried this, but you can set the environment variable OCAMLRUNPARAM to e.g. "s=1G" to set the minor heap size (in words, so here 1 G = 8 GiB).

1 Like

core_bench tries to reduce the impact of the GC on the measurements as much as possible. It might provide useful inspiration


One must be careful about interpreting the results of such benchmarks. One benefit of minor collection is to take a memory arena sparsely-populated by live blocks and reallocating these blocks close to each other, that is with better cache locality. Modern allocators such as the one that inspired multicore OCaml’s major allocator are explicitly designed to improve cache locality. In other words by “turning off” GC you are artificially decreasing the cache locality of your data.

As we saw in a previous thread, it is not clear that there is a meaningful notion of “program performance without the memory management overheads”.

1 Like

This might not be relevant with the new multicore GC, and it’s been a few years since I was playing with it, but IIRC the name of the C function that is called when a GC might be triggered is a weak symbol, and you can compile in a different implementation that replaces the function in the Ocaml runtime, to do whatever you want (like never actually trigger a GC).