Capping mono memory usage

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.

Advertisements

3 thoughts on “Capping mono memory usage

  1. This MONO_GC_PARAMS=max-heap-size=120M trick is incredible, I thought mono unlike java did not allow this, can we figure out a way to publize this more? This just kept an LSF queue system from killing all my jobs…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s