.net - 即使应用似乎没有占用最大内存量,也会发生OutOfMemoryException

标签 .net memory crash clr out-of-memory

我正在开发一个基于.NET的应用程序(.NET 4),该应用程序在 System.OutOfMemoryException 上运行一段时间后崩溃

当在任务管理器中检查应用程序的进程或尝试使用其他工具(例如CLR Profiler和ANTS Memory Profiler)时,似乎应用程序的可用RAM或其他任何限制都没有达到极限(我相信Windows具有每个进程的限制以及.NET本身可能以某种方式受到限制)。

这是崩溃之前不久的ANTS Profiler的屏幕截图。

未使用的内存总量(以防在图像中看不到的情况下为〜285 MB,并且所有堆中对象的总大小为〜76MB)

我还检查了taskmgr中的“Handles”计数,崩溃时该应用程序的进程看起来约为8120个句柄。

我如何进一步检查发生了什么?有哪些可能的原因会导致这种异常?

最佳答案

It's an image (Bitmap). It is not "very large" in size (around 50kb). Can this be some underlying native exception in disguise? (like something coming from GDI)



是的,这可能是原因。当出现与内存消耗无关的错误时,GDI通常会抛出OutOfMemoryException

我建议您检查要尝试加载的Bitmap,并验证其格式是否正确,且格式为GDI支持。

这是long term bug on Connect,其官方响应为:

you're seeing one of the idosyncracies of the drawing engine (GDI+) here. GDI+ likes to return OutOfMemoryExceptions in cases that have nothing to do with memory. Unfortunately GDI+ is a component that we get from Windows so we cannot address these issues



...

We often see these when you pass invalid parameters to a function...



检查加载Bitmap的调用以及图像文件的格式详细信息,可能会导致您找到“真正的”根本原因。

关于.net - 即使应用似乎没有占用最大内存量,也会发生OutOfMemoryException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18108082/

相关文章:

iphone - 滚动超过列表的开头或结尾时,UIPickerView 使应用程序崩溃

c# - string.Join - "cannot convert from IEnumerable to string[]"

.net - 带有 HttpWebRequest 的 HTTP POST

java - 老一代总是 100% > 导致 100% CPU 使用率

ios - 应用程序进入后台时 GCDWebServer 崩溃

c++ - wsregex::compile 在处理正则表达式字符串时崩溃(内存泄漏)?

c# - 如何使用 Unity 从配置文件注入(inject)构造函数参数

c# - JSON.NET - 如何为作为对象类型存储在数组/列表中的原始 C# 类型包含类型名称处理

python - 为什么 defaultdict(int) 的 dicts 使用这么多内存? (以及其他简单的 python 性能问题)

python - 在脚本调用之间保留 Python 变量