Benefits of OCaml

Garbage collection

In OCaml, the memory used to store a value is freed after the value is no longer referenced. The programmer does not then need to determine when values are unused and deallocate manually. This automatic freeing of memory is known as garbage collection (GC). More importantly the programmer no longer has to alter the design of a program to make deallocation easy.

GC is particularly important in the presence of functional programming, as closures often capture "local" variables and carry them out of the function they are "local" to.

Modern GCs are as fast as manual memory allocation and deallocation. OCaml's GC is no exception, allowing OCaml to give competitive performance on a wide range of tasks. For example, OCaml is one of the fastest languages in our ray tracer benchmark.

GC can be exploited to handle external resources, such as OpenGL display lists, by wrapping the handle to an external resource in an OCaml object and using the initializer of the object to set a finaliser that will deallocate the external resource. This is, strictly, an abuse of GC but it works very well in practice.

GC can be a concern when writing soft real-time applications, such as visualisation, because GCs incur stalls during the collection cycle. OCaml's GC is sufficiently incremental that high-end visualisation software does not suffer from stuttering associated with long stalls. Indeed, the worst case performance of OCaml's GC is typically only 1ms.

GC improves reliability and facilitates many of the other features that benefit OCaml, particularly functional programming.