内存不足异常+ C#Winforms [英] Out of memory exception + C# Winforms

查看:69
本文介绍了内存不足异常+ C#Winforms的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我在这里有疑问。我的应用程序基于多线程应用程序中的C#Winforms构建。最近在我的应用程序运行了很长时间后,我正面临一个活动窗口中的Out of memory exception并断言。可能是什么原因?我在这里使用PInvoke调用

。它会引起问题吗? GC在PIO呼叫上无法正常工作吗?请建议。

解决方案

GC工作正常,但这个问题与问题无关。

你认为用GC你是吗?没有内存泄漏?如果是这样,这是完全错误的。当然它可以消除由于忘记释放一些内存而导致的内存泄漏,但是如果你的设计错误就无法解决问题。设计泄漏托管内存的代码非常容易。



至于忘记释放一些资源,这仍然是其他类型内存泄漏的来源,非托管资源。特别是,您需要保证为实现 System.IDisposable 的所有类型正确调用 Dispose ,因为许多此类对象分配非托管资源并仅在您调用 Dispose 时释放它们。这是Mehdi在他的解决方案中提到的可能性。使用使用语句(不要与使用子句混淆)。



当然,您可以通过设计进行内存泄漏。一个典型的情况是将对象放在一些应用程序全局集合中(例如,为了快速搜索),并且在不再使用对象时忘记删除引用。顺便说一下,考虑使用 System.WeakReference ,参见 http://msdn.microsoft.com/en-us/library/system.weakreference.aspx [ ^ ]。



所以,首先要分析你的设计以便管理所有物体的生命周期。请记住,只有当整个应用程序域中对它的所有引用从工作代码中完全无法访问时,GC才会安排对象进行销毁,但它不适用于弱引用。这种机制如果非常狡猾:如果你有循环引用的孤立路径,GC就足够聪明,可以安排所有涉及对象的销毁。考虑 A 引用 B B 引用 C C 引用 A 。如果丢失了对这三个对象中的每一个的所有其他引用,则无论如何它们将被安排为由GC销毁。 销毁的标准是可达性,请参阅 http:// en。 wikipedia.org/wiki/Garbage_collection_%28computer_science%29#Reachability_of_an_object [ ^ ]。[结束编辑]相当整洁,不是吗?



之后你可能需要使用好的建议由Mehdi使用内存分析器。一个好的记忆探测器是一个非常强大的武器。



-SA


你最好的选择是使用内存分析器(RedGate有一个好的)来查看你的内存泄漏的位置,很难说你的描述是什么。



Pinvokes如果处理不当,可能会导致这种情况。


在处理多线程应用程序时编写代码的最佳方法是将代码块包含在使用中。这不是最好或最简单的方法,但会避免大多数与内存相关的问题。



告诉我们这个建议是否适用于您的情况

Hi All,

I have a doubt here. My application is built on C# Winforms in multi threaded application. Recently after a long run of my application, I am facing " Out of memory exception" from an active window and asserting. What might be the reason ? Here I am using PInvoke calls
. Will it causes issues ? Is GC will not work properly on PIO calls ? Please suggest.

解决方案

GC works properly, but this question has nothing to do with the problem.
Do you think that with GC you don't have memory leaks? If so, this is completely wrong. Of course it eliminates memory leaks you would make due to forgetting to release some memory, but it cannot help if you have wrong design. It's pretty easy to design code which leaks managed memory.

As to forgetting to release some resources, this is still a source of other kind of memory leaks, of unmanaged resources. In particular, you need guarantee that you properly call Dispose for all types implementing System.IDisposable, because many of such objects allocate unmanaged resource and free them only if you call Dispose. This is the possibility Mehdi mentioned in his solution. Use using statement (not to be mixed up with using clause).

Of course you can have memory leaks by design. One typical case is putting objects in some application-global collection (for the sake of fast search, for example) and forgetting to remove references when object is not used anymore. By the way, think about using System.WeakReference, see http://msdn.microsoft.com/en-us/library/system.weakreference.aspx[^].

So, first analyze your design for management of life cycle of all objects. Remember that an object is scheduled for destruction by GC only if all references to it in whole Application Domain becomes totally inaccessible form working code, but it does not apply to weak references. This mechanism if very cunning: if you have isolated path of circular reference, GC is clever enough to schedule all involving objects for destruction. Consider A referencing B, B referencing C and C referencing A. If all other references to each of these three objects are lost, they will be scheduled for destruction by GC anyway. [EDIT] The criteria for destruction is reachability, see http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29#Reachability_of_an_object[^].[END EDIT] Pretty neat, isn't it?

After that you may need to use the good advice by Mehdi and use a memory profiler. A good memory profiler is quite a powerful weapon.

—SA


Your best bet is to use a memory profiler (RedGate has a good one) to see where your memory leaks are, it is hard to say what it could be from your description.

Pinvokes could cause this if not disposed properly.


The best way to code while working on multi-threaded apps is to enclose your code blocks into 'using'. This is not ofcourse the best or fool-proof method, but will avoid most of the memory related problems.

Tell us if the suggestion works in your case


这篇关于内存不足异常+ C#Winforms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆