C# 内存不足异常-警告策略

发布于 2024-12-31 22:00:02 字数 366 浏览 3 评论 0原文

在复杂的多线程应用程序中,我可能每周都会遇到一次内存不足异常。应用程序通过多个套接字发送/读取大量数据,其中读取的数据被缓存以避免网卡缓冲区溢出。
分析内存异常的最佳策略是什么?在正常运行时,应用程序在进程资源管理器中显示的“所有堆中的总字节数”最大为 1.5 GB。
有一个正在轮询的线程是否是一种策略

GC.GetTotalMemory()

PrivateMemorySize64()

每秒一次以了解何时开始分析事物?我还没有研究过商业分析器,我有点担心它们对性能的影响,这可能会在实际问题分析方面给出错误的结果。

Inside a complex multithreaded application I am hitting an out-of-memory exceptions maybe once a week. The application is sending/reading massive amounts of data via several sockets, where the read data gets cached to avoid network card buffer overruns.
What is the best strategy to analyse the memory exceptions? During normal run-time, the App is shown with a size of "Total bytes in all Heaps" of up to 1.5 Gigabyte in the Process explorer.
Could it be a strategy to have a thread which is polling either

GC.GetTotalMemory()

or

PrivateMemorySize64()

once a second to know when to start analyzing things? I have not looked into commercial profilers yet and I am a bit concerned of the performance impact of them which could give also wrong results regarding the actual problem analysis.

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

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

发布评论

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

评论(4

独孤求败 2025-01-07 22:00:02

您的内存可能因大量字符串操作或其他创建和释放小内存块的操作(例如装箱/拆箱)而产生碎片。

当 CLR 无法分配足够大的空闲内存块时,您将收到此异常。

我使用“CLR Profiler”并检查内存分配。如果您看到大量白点(空闲块)并且没有大的空闲块,那么您需要开始查看如何分配对象。

例如,在将一个字符串分配给另一个字符串之前,首先检查字符串是否不同。使用 StringBuilder 是所有情况,消除装箱和其他内存优化。

我使用这种技术并完全消除了异常,除了二进制反序列化的已知问题。

重新发现托管代码中丢失的内存优化艺术,网址为 http://msdn.microsoft.com/en-us/magazine/cc163856.aspx

调查内存问题,位于 http://msdn.microsoft.com/en-us/magazine/cc163528.aspx

Visual Basic .NET 中的性能优化,位于http://msdn.microsoft.com/en-us/library/aa289513(v=vs.71).aspx

Your memory is probably being fragmented from numerous string operations or other operations that create and release small blocks of memory, such as boxing/unboxing.

You will get this exception when the CLR cannot allocate a large enough free block of memory.

I use the "CLR Profiler" and check the memory allocations. If you see numerous white-spots (free blocks) and no large free blocks then you need to start looking at how you are allocating objects.

For instance, before assigning one string to another, check to see if the strings are different first. Using StringBuilder is all cases, eliminate boxing, and other memory optimizations.

I use this technique and completely eliminated the exceptions, except for a known issue with binary de-serialization.

Rediscover the Lost Art of Memory Optimization in Your Managed Code at http://msdn.microsoft.com/en-us/magazine/cc163856.aspx

Investigating Memory Issues at http://msdn.microsoft.com/en-us/magazine/cc163528.aspx

Performance Optimization in Visual Basic .NET at http://msdn.microsoft.com/en-us/library/aa289513(v=vs.71).aspx

青柠芒果 2025-01-07 22:00:02

您可以考虑安装适用于 Windows 的调试工具并使用 adplus

ADPlus.vbs (ADPlus) 是 Microsoft 产品支持服务 (PSS) 提供的一个工具,可以对任何停止响应(挂起)或失败(崩溃)的进程或应用程序进行故障排除。

基本上,您可以设置监视应用程序,当应用程序崩溃时,它将捕获转储,然后您可以使用 WinDBG/SOS 进行分析。

You might consider installing the debugging tools for windows and using adplus

ADPlus.vbs (ADPlus) is a tool from Microsoft Product Support Services (PSS) that can troubleshoot any process or application that stops responding (hangs) or fails (crashes).

Basically, you can set that watching the application, and when it crashes, it will capture a dump, that you can then analyse using WinDBG/SOS.

挽心 2025-01-07 22:00:02

您可以使用 MemoryFailPoint 来尝试给您一些保证给定的操作

但我建议隔离您的应用程序正在运行的进程,如果可以的话,迁移到 64 位,并且您可能需要考虑降低一些性能,以便为您的内存使用提供合理的保证。

You can use a MemoryFailPoint to try and give you some guarantees on a given operation

But I recommend isolating the process that your app is running in, moving to 64 bit if that's an option and you may need to look at reducing some performance to give you reasonable guarantee's of your memory usage.

小猫一只 2025-01-07 22:00:02

使用弱引用进行缓存怎么样?
点击

How about using weak references for caching?
clicky

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