Reputation: 1730
I have a .NET process that, for reasons I will not go into, consumes a large amount of RAM. What I want to do is implement an upper limit on the amount of RAM this process can use. Is there a way to do this?
The closest I have found is Process.GetCurrentProcess().MaxWorkingSet
which I don't think does what I want based on the documentation I have read.
The best I have come up with is using GC.GetTotalMemory(true)
to get the amount of RAM being used and restart if it is over a certain amount. But this is not ideal.
Upvotes: 2
Views: 2044
Reputation: 134125
You can set the minimum and maximum sizes of the process's working set by calling SetProcessWorkingSetSizeEx or SetProcessWorkingSetSize.
There is a pinvoke.net entry for SetProcessWorkingSetSize.
Be sure to read the MSDN documentation for whichever function you decide to call.
Note that this doesn't prevent your application from allocating more virtual memory, but rather puts limits on how much physical RAM the process's virtual memory occupies at any given time.
Upvotes: 2