.NET 中 OutOfMemoryException 的 try/catch 块的设计模式

发布于 2024-11-19 17:48:23 字数 169 浏览 2 评论 0 原文

我有一个处理大量数据的应用程序,我想,有时可能会抛出 OutOfMemoryException (半年来,我没有遇到任何异常,但我'我只是想知道这一切)。正如我所调查的,发生此异常后我无法继续执行我的程序。

是否有任何好的模式来处理此类异常,尤其是使用 IDisposable 类?

I have an application that works with large amounts of data, and I'm thinking that, may be, sometimes the OutOfMemoryException will be thrown (For half a year, I got no single exception, but I'm just want to know all about it). As I've investigated, after this exception I can't proceed with execution of my program.

Is there any good pattern to handle such exceptions, especially to working with IDisposable classes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

微凉徒眸意 2024-11-26 17:48:23

在真正的 OOM 场景中(在 x86 上比 x64 上更有可能),你注定会失败。几乎任何事情都会导致分配,所以你最好的选择是尽可能快地、优雅地死去,把伤害降到最低。

既然它没有发生,就不要过度强调,但是避免比在这里处理更好:

  • 使用流数据 API,而不是将所有内容缓冲在内存中
  • ,重用缓冲区等,
  • 避免巨大的数组/列表等(事实上​​,导致 OOM 的最有可能的方法是请求一个巨大的(但单个)数组) - 例如,锯齿状数组的扩展性比 2D 数组更好(即使在 x64 上,也对最大大小有硬性限制)一个单一的array)
  • 考虑一下如何处理稀疏数据
  • 您是否从外部源读取大量字符串?如果是这样,请考虑使用自定义内部函数,这样您就不会拥有 20,000 个不同的通用字符串副本(例如国家/地区名称),
  • 时密切关注您释放的内容
  • 并在避免意外延长对象寿命 ,尤其是通过事件订阅(因意外延长生命周期而臭名昭著)

In a genuine OOM scenario (more likely on x86 than x64) you are pretty doomed there. Almost anything would cause an allocation, so your best option is to die as quickly and elegantly as possible, doing minimum harm.

Since it isn't happening, don't stress overly, but avoidance is better than handling here:

  • use streaming data APIs rather than buffering everything in memory
  • re-use buffers etc
  • avoid enormous arrays/lists/etc (in truth, the most likely way to cause an OOM is to request an enormous (but single) array) - for example, a jagged array scales better than a 2D array (even on x64 there is a hard limit on the maximum size of a single array)
  • think about how you handle sparse data
  • do you read lots of strings from external sources? If so, consider around a custom interner so you don't have 20,000 different copies of common strings (country names, for example)
  • keep an eye on what you release when
  • avoid accidentally prolonged life on objects, especially via event subscriptions (notorious for accidental extensions to lifetimes)
_蜘蛛 2024-11-26 17:48:23

没有好的模式,OOM 是一种随时可能发生的令人讨厌的异常。这使得它几乎成为一个异步异常。处理它的唯一可能性是当您的代码被构造为同时分配大量内存时。因此,您将有可能退出并展开程序状态,就好像什么也没发生一样。

您需要设计您的程序,使其永远不需要分配超过所有可用虚拟内存的一半(在 32 位计算机上为 1 GB)。 Dragons 的寿命超过了这个数量,即使还有 500 MB 的虚拟内存未使用,您的程序也会在分配 90 MB 或更少的内存时失败。地址空间碎片引起的问题。如果您经常超过此阈值,那么您需要切换到 64 位操作系统。

There is no good pattern, OOM is a nasty exception that can strike at any moment. Which makes it almost an asynchronous exception. The only odds you have for handling it is when your code is structured to allocate large amounts of memory at the same time. So you'll have some odds to back out and unwind the program state as though nothing happened.

You need to design your program so it never needs to allocate more than about half of all available virtual memory, one gigabyte on a 32-bit machine. Dragons live beyond that amount, your program will fail on an allocation of 90 MB or less, even if there is another 500 MB of virtual memory unused. A problem induced by address space fragmentation. If you routinely cross this threshold then you need to switch to a 64-bit operating system.

往事风中埋 2024-11-26 17:48:23

我面前的两个答案是正确且合理的建议,
但有一件事他们没有提到——那就是负载测试。
如果您担心在特定负载下您的应用程序可能会耗尽内存,请针对该负载(最好是更大的负载)对其进行测试。

在我之前的工作中,我使用过这样的工具(HP 性能中心),事实证明,它不仅对于检查我们系统的错误和限制,而且对于识别瓶颈和昂贵的运营也非常有价值。

The two answers before mine are correct and sound advice,
but there's one thing they haven't mentioned - which is load testing.
If you're concerned that under certain load your application might run out of memory- test it against that load (and preferably- larger loads).

In my previous job I used such a tool (HP's Performance Center) and it proved invaluable not only to check for errors and limits of our system, but also in identifying bottlenecks and costly operations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文