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
Disabling the OCaml garbage collector
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2007-11-28 (18:05)
From: Raj <rajb@r...>
Subject: Re: [Caml-list] Disabling the OCaml garbage collector
The issue for me is that I need to be able to modify mutable objects in 
OCaml (eg. array-modification in-place) from both OCaml and C/Python. 
However, the OCaml GC moves things around while the execution is in 
C/Python and this crashes my program.

The Python GC is very simple, based on reference counts, hence (I think) 
it suffices just to make sure that any object has a refcount > 0 to keep 
the Python GC off.

Well, I decided to go ahead and hack some C code to build a custom OCaml 
compiler. These are the steps I followed:

1) Add the following declarations to stdlib/ and stdlib/gc.mli

external disable: unit -> unit = "caml_gc_disable"
external enable: unit -> unit = "caml_gc_enable"

2) Modify byterun/gc_ctrl.c and implement the C functions. For example,

int gc_enabled = 1; //global variable

CAMLprim value caml_gc_enable(){
        gc_enabled = 1;

3) Modify the "main loop" in gc_ctrl.c as

CAMLprim value caml_gc_compaction(value v)
if(gc_enabled) {
  Assert (v == Val_unit);
  caml_empty_minor_heap ();
  caml_finish_major_cycle ();
  caml_finish_major_cycle ();
  caml_compact_heap ();
  caml_final_do_calls ();
  return Val_unit;

However, when I compile this code with 'make world' I get the following 
error while linking:

boot/ocamlrun boot/ocamlc -nostdlib -I boot  -linkall -o ocaml.tmp 
toplevel/toplevellib.cma toplevel/topstart.cmo
Error while linking boot/stdlib.cma(Gc):
The external function `caml_gc_enable' is not available
make: *** [ocaml] Error 2

Any ideas what I am missing?


> You definitely may need to hack the C code inside byterun/minor_gc.c 
> (for example), but I don't understand what you want exactly.
> However, I do know that mixing GCs, like Ocaml & Python, is a nightmare.
> Did you consider having two separate Unix processes, one for the Ocaml 
> code and one for the Python code, and having them communicate thru 
> standard stuff like pipes, memory mapped files, ...