package dynamic_gc
Install
Dune Dependency
Authors
Maintainers
Sources
md5=072dbfbbb154bffbc1dd927e86631352
sha512=e30da32d2dc1413b201678cb21d802934db750c21a1ad3f3dd75902ea998296df77ad3f3f401f4c9bc6c4bae958246f3be2b518e2fd15dd08cf9a66a32d9666d
Description
A utility to dynamically adjust garbage collector behavior based on memory usage, allowing your application to prioritize improved run time when memory usage is low, but prioritize decreased memory usage when memory usage is high.
README
Dynamic GC
Dynamic tuning for the OCaml garbage collector.
This utility allows you to instruct the OCaml garbage collector to become more aggressive as the size of the major heap grows. This can be useful if you would prefer your application to execute quickly with few resources dedicated to garbage collection when memory usage is low, but would like to change that trade-off when memory usage is high.
Specifically, this utility adjusts the space_overhead
value at the end of each major collection using the Gc
module.
Usage
Install via opam install dynamic_gc
.
Use according to DynamicGc.mli
.
For example, you might put the following at or near the entry point to your program. It would allow space_overhead
to range between 20 and 40, such that it is 40 when the size of the major heap is less than 2 GB, 20 when the size of the major heap is greater than 4 GB, and linearly interpolated in between.
DynamicGc.(setup_dynamic_tuning
{
min_space_overhead = 20;
max_space_overhead = 40;
heap_start_worrying_mb = 2_048;
heap_really_worry_mb = 4_096;
});
Caveats
This tunes the garbage collector based on the size of the major heap, not the amount of memory that is currently live. Because OCaml 5 does not do compaction by default, this means that if your program uses a lot of memory and then releases it, the garbage collector may still be set to an aggressive setting because the major heap is still large. You may need to manually call Gc.compact ()
in order to free memory and reduce the size of the major heap, thereby allowing this utility to increase the value of space_overhead
.
This utility relies on garbage collector alarms. This functionality is broken in OCaml 5.2.0. Upgrade to 5.2.1 instead to use this utility.
Contributing
To build: dune build
To test: dune test
To use your local copy in another project: opam pin .