I have a need (eventually on my Pi) to limit the amount of memory a mono process uses up.
I have some mixed (managed with native pinvoke) programs. We have other programs too that are running on the same system and (eg it is a Pi) it doesnt have much RAM and zero swap.
One mono process will freely gobble up as much memory as it can (and occasionally running garbage collections at the right momens) until it can’t allocate more (and the GC kicks in again). Say you have a Linux system like this:
- 256 Mb RAM
- 0 MB Swap
- 3-4 long lived mono programs running
It might be reasonable to enforce limits using ulimit and setting hard limits on memory allocation but that will apply to the whole process. If you have portions of unmanaged code they may not be well written to deal with low or out of memory conditions and your program may well just abort or crash when you are close to your memory limit.
One potential answer to this is capping the managed heap size by setting the following environment variable before your program starts:
export MONO_GC_PARAMS=max-heap-size=120M mono myserver.exe
If you set your ulimit values to say, 160Mb then there should still be a fair amount of space left for your unmanaged libraries to allocate in.