有时,使应用程序进入不良状况以查看其响应方式会很有帮助。拔下网络电缆或断电之类的事情将向我展示我的应用程序的弹性和工作要做的地方。
为此,我正在尝试找出OutOfMemoryException
在.Net中强制执行的最快方法。在一个简单的控制台应用程序中执行此操作,将使我能够将这种情况注入正在运行的应用程序中。显然,在处理时还需要考虑其他事项OutOfMemoryExceptions
(例如,内存碎片以及垃圾回收器如何分配不同的世代),但这对于本实验的范围并不重要。
更新
为了阐明问题的目的,重要的是要注意,仅仅抛出内存不足异常并没有帮助,因为我想了解当内存压力增加时程序将如何反应。从本质上讲,我希望将GC激进为积极的收集模式,并监视该过程如何影响性能,直到该过程因内存不足异常而终止。
一个例子来自MSDN ..
下面的示例说明了示例尝试插入导致对象的Length属性超过其最大容量的字符串时,调用StringBuilder.Insert(Int32,String,Int32)方法引发的OutOfMemoryException异常。
using System;
using System.Text;
public class Example
{
public static void Main()
{
StringBuilder sb = new StringBuilder(15, 15);
sb.Append("Substring #1 ");
try {
sb.Insert(0, "Substring #2 ", 1);
}
catch (OutOfMemoryException e) {
Console.WriteLine("Out of Memory: {0}", e.Message);
}
}
}
// The example displays the following output:
// Out of Memory: Insufficient memory to continue the execution of the program.
此外,它还介绍了如何纠正错误。
将对StringBuilder.StringBuilder(Int32,Int32)构造函数的调用替换为任何其他StringBuilder构造函数重载的调用。StringBuilder对象的最大容量将设置为其默认值,即Int32.MaxValue。
使用maxCapacity值调用StringBuilder.StringBuilder(Int32,Int32)构造函数,该值足够大以容纳对StringBuilder对象的任何扩展。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句