问题
I have an application that must use a library that I didn't write and I don't have the power to change it. Basically there is a memory leak, so the long it runs, the more it leaks; it basically just writes stale pagefiles that I don't have the power to delete. The memory leak doesn't actually cause the program to crash, it just simply eats up all the memory. When it is using all the system's memeory, the OS will just start deleting the old page files and everything runs fine.
If I run my app on a 2gb system, it will use 2 gb and continue to run; same for a 16gb system.
Is there a way to set the amount of memory an application can use?
回答1:
That is a per-process limitation in Windows. Each process gets ~2GB virtual address space (that is what Heap makes use of) in a 32-bit machine. And I am afraid there is not much you can do about it.
Roughly, CLR is able hold upto ~1.6GB of objects in memory. That should be enough for most of the applications. If not, then you need to work on your application.
In my case, I faced a similar problem and then used SqlDataReader to fetch objects in a specified chunk size, process it, compute it, clean it from memory, and then fetch another chunk.
There is also a detailed article on MSDN - Investigating Memory Issues
Hope this would be helpful.
回答2:
Have also a look at Process Governer: http://lowleveldesign.wordpress.com/2013/11/21/set-process-memory-limit-with-process-governor/. I wrote this tool to test memory leaks in my applications. The process running with a memory limit will throw OutOfMemory if it exceeds it.
来源:https://stackoverflow.com/questions/8966639/set-maximum-memory-usage-c-sharp