7

imagine you have an application (single process) written in c#

By default the application is allocating huge virtual memory, far more than it needs (for example the resident memory is about 10mb, while virtual memory is about several GB).

In Java it's possible to use an option to limit this with java -mx128m

How can I do the same for .net / mono application? And the bonus question: is it possible to enforce / change the GC options to always keep as low memory usage as possible (collect as soon as possible, not when system is running out of memory)

basically my goal is to create a .net application with minimal memory footprint

EDIT: in order to make it more clear I insert this scenario, given that nearly every .net application seems to have similar problem:

I run a monodevelop, which is c# application under mono - it eats 1800 MB of virtual memory, while it only uses 291 MB of resident memory. That means the application is using only less than 300mb of memory but it asked for 1800MB for unknown reason. This reason is what I would like to discover as well as the way how to prevent it doing so.

That probably isn't problem for most of users, but it may be problem for applications running on clusters like SGE when task is limited by virtual memory.

Petr
  • 13,747
  • 20
  • 89
  • 144
  • Wouldn't it be better to figure out ***why*** the application is allocating so much virtual memory? Rather than just setting a switch that starves it for memory? – Cody Gray - on strike Apr 07 '13 at 11:36
  • you didn't understand the question, or maybe I didn't ask properly. This is not about certain 1 application, it's about all .net applications in general. Java has this switch that set a limit for virtual memory forcing it to use as less memory as possible. That is useful on advanced clusters such as SGE where task is limited by virtual memory. My question is if there is similar possibility for .Net so that GC gets in situation when it is running out of memory sooner than the whole system does – Petr Apr 07 '13 at 14:56
  • @CodyGray the application is of course allocating so much memory because that's natural for .net, just as it is natural for any other language which is using garbage collector - it doesn't free up memory unless it needs to free up memory and it allocates lot of virtual memory, while using only small resident memory, simple "Hello world" applications in java can eat up to several GB of operating memory - it depends on configuration of interpreter - see http://stackoverflow.com/questions/561245/virtual-memory-usage-from-java-under-linux-too-much-memory-used – Petr Apr 07 '13 at 14:59
  • VM usage does not use up memory resources. It is a logical concept. No point fighting it. When it comes to .NET you should look at private bytes because those are actually the amount of memory used. Not sure what you mean by "resident" but if you mean "working set" then it means nothing. – usr Apr 07 '13 at 15:07
  • I am talking about UNIX in my scenario - resident memory is similar to working set in windows. – Petr Apr 07 '13 at 15:10
  • I don't want to fight anyone :) I just want to understand this – Petr Apr 07 '13 at 15:11
  • 2
    at some point it would be more useful if instead of downvoting you told me what is not clear on the question, or is it so hard to answer this? This is a real problem - clearly every .net / mono application is facing it, per my example with monodevelop. Even very simple application that eats about 30mb of resident memory is eating up to 2gb of virtual memory. Why? – Petr Apr 08 '13 at 13:44
  • If you're working in a memory constrained environment, the garbage collection algorithm is tuned differently. I still don't understand what problem you're trying to solve here. You've just presented a long discussion of how garbage collection works. – Cody Gray - on strike Apr 08 '13 at 20:28
  • 1
    I have several problems with this behaviour of .Net, one of them is that I have relatively simple application that is eating less than 50mb of resident memory. However I need to run it on SGE cluster where memory is restricted. By default to 200mb of virtual memory. This mono application isn't able to start however, unless the virtual memory limit is not set to at least 900mb. Why in the world do I have to request 900mb of operating memory for something that never eats more than 100mb of resident memory? – Petr Apr 09 '13 at 10:29

1 Answers1

3

Mono has several flags for setting an upper limit on memory like java. For example, when I run my cluster jobs I set the following environmental variable.

setenv MONO_GC_PARAMS max-heap-size=8G

you can see more options available in this question here:

Mono GC max-heap-size isn't documented. Is it safe to use in production?

Community
  • 1
  • 1
evolvedmicrobe
  • 2,672
  • 2
  • 22
  • 30