我需要重构我的项目,以使其免受 OutOfMemory
异常的影响。
我的项目中使用了巨大的集合,通过更改一个参数,我可以使我的程序更加准确或使用更少的内存......
好吧,这就是背景。我想做的是循环运行例程:
- 使用默认参数运行子例程。
- 捕获
OutOfMemory
异常,更改参数并尝试再次运行。
- 执行第二点,直到参数允许运行子例程而不引发异常(通常,只需要进行一项更改)。
现在,我想测试一下。我知道,我可以自己抛出 OutOfMemory 异常,但我想模拟一些真实情况。
所以主要问题是:
有没有办法为我的程序设置某种内存限制,达到该限制后将自动抛出 OutOfMemory
异常?
例如,我想为我的整个程序设置一个限制,比如说400MB内存,以模拟系统中有这么多可用内存时的情况。
能做到吗?
I need to refactor my project in order to make it immune to OutOfMemory
exception.
There are huge collections used in my project and by changing one parameter I can make my program to be more accurate or use less of the memory...
OK, that's the background. What I would like to do is to run the routines in a loop:
- Run the subroutines with the default parameter.
- Catch the
OutOfMemory
exception, change the parameter and try to run it again.
- Do the 2nd point until parameters allow to run the subroutines without throwing the exception (usually, there will be only one change needed).
Now, I would like to test it. I know, that I can throw the OutOfMemory
exception on my own, but I would like to simulate some real situation.
So the main question is:
Is there a way of setting some kind of memory limit for my program, after reaching which the OutOfMemory
exception will be thrown automatically?
For example, I would like to set a limit, let's say 400MB of memory for my whole program to simulate the situation when there is such an amount of memory available in the system.
Can it be done?
发布评论
评论(6)
这看起来像是...System.Runtime.MemoryFailPoint 的工作。
http://msdn.microsoft.com/en-us/ library/system.runtime.memoryfailpoint.aspx
我认为链接中的示例适合您的情况。将 MemoryFailPoint 设置为您需要的任何级别,然后捕获 InsufficientMemoryException 并相应地调整您的输入参数。
This looks like a job for...System.Runtime.MemoryFailPoint.
http://msdn.microsoft.com/en-us/library/system.runtime.memoryfailpoint.aspx
I think the example in the link fits your situation. Set the MemoryFailPoint to whatever level you need and then catch the InsufficientMemoryException and adjust your input parameters accordingly.
应该不会花很长时间,除非你有 12GB 的内存:)
Shouldn't take long unless you've got 12gb of ram :)
只需分配一个非常大的数组即可。一旦您的 C# 应用程序的 RAM 使用量达到 1.2-1.6GB(通常位于该范围的较低值,前提是其目标为 x86),您很可能会开始出现内存不足异常。
Just allocate a very large array. You'll most likely start getting out of memory exceptions once your C# application reaches 1.2-1.6GB of RAM usage (usually on the lower side of that range, provided its targetting x86).
我想建议另一种看待这个问题的方式。您不一定要耗尽内存。您只需要监视所使用的内存量,并将其与系统总内存进行比较。也许类似 GC.GetTotalMemory 在这里会很有用查看您的应用程序使用了多少内存。那么也许是这篇文章将有助于获取系统上可用的物理 RAM 总量。
I'd like to suggest another way of looking at this. You don't necessarily have to run out of memory. You just need to monitor the amount of memory used, and compare it to the total system memory. Perhaps something like GC.GetTotalMemory will be useful here to see how much memory your application is using. Then perhaps this article will help with getting the total amount of physical RAM available on your system.
模拟内存不足异常的一个好方法是在虚拟机 (VM) 中运行程序。您可以将虚拟机的最大内存分配设置为足够小的级别,以免对程序造成内存压力并引发异常。
一些开源虚拟机是:QEMU、xen 和 kvm。
一个好的商业虚拟机是适用于 Mac OS X 的 VMware Fusion 或适用于 Linux/Windows 的 VMware Player。
A good way to simulate an out-of-memory exception is to run the program in a virtual machine (VM). You can set the maximum memory allocation for the virtual machine to a level that is small enough to create memory pressure on your program and provoke the exception.
A few open source VMs are: QEMU, xen, and kvm.
A good commercial virtual machine is VMware Fusion for Mac OS X or VMware Player for Linux/Windows.