Can I configure my C# application to limit its memory consumption to, say, 200MB?
IOW, I don't want to wait for the automatic GC (which seems to allow the heap to grow much more than actually needed by this application).

I know that in Java there's a command line switch you can pass to the JVM that achieves this.. is there an equivalent in C#?

p.s.

I know that I can invoke the GC from code, but that's something I would rather not have to do periodically. I'd rather set it once upon startup somehow and forget it.

Is there a particular reason you need to do this? Garbage collectors operating in an artificially constrained memory environment tend to perform worse that those allowed to use more memory.
–
Sam HarwellOct 28 '09 at 8:05

1

In this particular case there are other (native) applications living on the machine that cannot afford the GCed app taking so much memory.
–
Assaf LavieOct 28 '09 at 12:32

This is the observed behavior: the system runs in a loop and the working set size grows indefinitely. When GC is run explicitly, in code, every once in a while, the working set size remains constant. I realize that there's something fishy going on here, but the fact that running GC keeps the WS constant tells me that the application is not holding on to objects.
–
Assaf LavieOct 28 '09 at 12:34

Keep in mind that the CLR manages OS allocated memory on behalf of your application. Thus you cannot necessarily expect to see an immediate drop in working set size after cleanup. The GC tunes itself for better performance, and if segments of memory are not needed, they will eventually be released to the OS.
–
Brian RasmussenOct 28 '09 at 12:39

The memory manager lets the host
provide an interface through which the
CLR will request all memory
allocations. It replaces both the
Windows® memory APIs and the standard
C CLR allocation routines. Moreover,
the interface allows the CLR to inform
the host of the consequences of
failing a particular allocation (for
example, failing a memory allocation
from a thread holding a lock may have
certain reliability consequences). It
also permits the host to customize the
CLR's response to a failed allocation,
ranging from an OutOfMemoryException
being thrown all the way up through
the process being torn down. The host
can also use this manager to recapture
memory from the CLR by unloading
unused app domains and forcing garbage
collection. The memory manager
interfaces are listed in

That doesn't sound like something I'd want to do. I was hoping for a configurable switch, like in Java. In fact, I'm scratching my head trying to figure out why there wouldn't be such a switch. I mean, somewhere, something is deciding when to run GC based on some limit/heuristic, and it seems reasonable to want to be able to configure that limit. Again.. Java allows this..
–
Assaf LavieOct 28 '09 at 12:37

I haven't tried this out, but you could attempt to call SetProcessWorkingSetSizeEx passing in the right flags to enforce that your process never gets more than so much memory. I don't know if the GC will take this into account and clean up more often, or if you'll just get OutOfMemoryExceptions.