有时,将应用程序置于不良状态以查看其响应方式会很有帮助.拔掉网线或断电等事情将向我展示我的应用程序的弹性以及我的工作地点.
为此,我试图找出强制OutOfMemoryException进入.Net 的最快方法.在简单的控制台应用程序中执行此操作将允许我将此方案注入正在运行的应用程序中.处理时显然还需要考虑其他事项OutOfMemoryExceptions(例如内存碎片以及垃圾收集器如何分配不同代)但这对于本实验的范围并不重要.
更新
为了澄清问题的目的,重要的是要注意只是抛出一个内存不足的例外是没有用的,因为我想看看当内存压力增加时程序将如何反应.从本质上讲,我希望将GC激发为积极的收集模式,并监视它对性能的影响,直到该过程因内存不足异常而消失.
MSDN 的一个例子..
\n\n\n下面的示例说明了当示例尝试插入会导致对象的字符串时,调用 StringBuilder.Insert(Int32,\xe2\x80\x82String,\xe2\x80\x82Int32) 方法引发的 OutOfMemoryException 异常长度属性超过其最大容量
\n
using System;\nusing System.Text;\n\npublic class Example\n{\n public static void Main()\n {\n StringBuilder sb = new StringBuilder(15, 15);\n sb.Append("Substring #1 ");\n try {\n sb.Insert(0, "Substring #2 ", 1);\n }\n catch (OutOfMemoryException e) {\n Console.WriteLine("Out of Memory: {0}", e.Message);\n }\n }\n}\n// The example displays the following output:\n// Out of Memory: Insufficient memory to continue the execution of the program.\nRun Code Online (Sandbox Code Playgroud)\n此外,它还说明了如何纠正错误。
\n\n\n将对 StringBuilder.StringBuilder(Int32,\xe2\x80\x82Int32) 构造函数的调用替换为调用任何其他 StringBuilder 构造函数重载。StringBuilder 对象的最大容量将设置为其默认值,即 Int32.MaxValue。
\n使用足够大的 maxCapacity 值调用 StringBuilder.StringBuilder(Int32,\xe2\x80\x82Int32) 构造函数,该值足以容纳对 StringBuilder 对象的任何扩展。
\n
| 归档时间: |
|
| 查看次数: |
210 次 |
| 最近记录: |