托管内存泄漏 [英] Managed memory leak

查看:74
本文介绍了托管内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常奇怪的内存泄漏,似乎与数据绑定有关.复制非常困难,因此我不会在此处发布任何代码,而只是描述问题.

我们有数据输入表单,其中包含控件,这些控件可通过BindingSource绑定到自定义业务对象目的.在处理该表单时,我们按如下所述调用ClearBindings:
http://msdn.microsoft .com/library/default.asp?url =/library/zh-cn/dnwinforms/html/databinding_winforms10_11.asp#databinding_winforms10_11_topic11

但是,关闭表单后,我们的业务对象仍保留在内存中.使用内存概要分析工具,我可以看到我们业务对象的根路径:

[业务对象]
System.Collections Hashtable.bucket []
System.Collections Hashtable
System.ComponentModel ReflectPropertyDescriptor
System.ComponentModel PropertyDescriptor []
System.Collections Hashtable.bucket [] Collections Hashtable
System.ComponentModel ReflectTypeDescriptionProvider _propertyCache
我尝试用我所知道的所有方式调用TypeDescriptor.Refresh,但是没有清除对象的缓存.顺便说一句,这是与.NET 2.0一起使用的.

任何帮助,我们将不胜感激!

I am having a very strange memory leak that seems related to databinding. It is very hard to reproduce, so I won't post any code here to do so, but will just describe the problem.

We have data entry forms which have controls which bind to custom business objects through a BindingSource object. On Dispose of the form, we call ClearBindings as described in:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwinforms/html/databinding_winforms10_11.asp#databinding_winforms10_11_topic11

However, our business object stay in memory after the form has been closed. Using a memory profiling tool, I can see that there is a root path to our business object:

[Business Object]
System.Collections    Hashtable.bucket[]  
System.Collections    Hashtable  
System.ComponentModel    ReflectPropertyDescriptor   
System.ComponentModel    PropertyDescriptor[]
System.Collections    Hashtable.bucket[]
System.Collections    Hashtable
System.ComponentModel    ReflectTypeDescriptionProvider    _propertyCache


I have tried calling TypeDescriptor.Refresh every way I know, but the cache of our object is not clearing out. BTW this is with .NET 2.0.

Any and all help will be appreciated!

推荐答案

就像这里有一个要点-处理完表单后,对象可能会保留在内存中很长时间.这是因为.NET使用了一个称为垃圾回收过程的过程,该过程将定期出现,并在内存中找到所有不可引用的对象并回收内存.

So if you objects are no longer referencable in you code then eventually they will be recovered.

因此,如果您的对象在代码中不再可引用,那么最终它们将被恢复.

When does this garbage collection process happen ?   This is a interesting point in that it happens when the system is short on resources and needs to try and find some more - so it will run and clean up memory, It would also occur periodically although not really at pre-determined time periods.

此垃圾收集过程何时发生?这是一个有趣的观点,它发生在系统资源短缺并且需要尝试查找更多资源的时候-这样它将运行并清理内存.尽管实际上不是在预定的时间段内,它也会定期发生. /p>

那为什么这样好呢,因为在使用引用计数的COM下,如果发生崩溃并且内存不再具有引用或具有某种形式的循环引用,这可能会导致内存泄漏,这意味着内存将被永久持有.垃圾收集过程不使用引用计数,而是在搜索不再可引用的对象并将回收内存.

So why is this good, because under COM which used reference counting you could get memory leaks if something crashed and the memory not longer had a reference or you had some form of cyclic reference which would mean the memory would be formever held.    The garbage collection process does not use reference counting and is searching for objects that are no longer referencable and will reclaim the memory.

所以您的内存泄漏可能只是尚未被收集.我会猜测,这就是为什么它难以复制的危险.如果您有大量的资源(内存),那么垃圾回收过程的运行频率可能会降低很多,因为它的运行频率没有那么短.如果您有很多正在运行的进程-资源变得越来越重要,并且该进程将更频繁地运行.

So you memory leak is probably just that its not yet been collected.    I would hazard the guess thats why its difficult to reproduce.   If you've got loads of resources (memory) then the garbage collection process will probably run a lot less frequently as it doesnt run short as often.    If you have lots of processes running - resources become more important and the process will run more frequently.

有大量参考资料,只需在垃圾回收+ .NET上进行网络搜索

There is loads of reference material, just do a web search on garbage collections + .NET


这篇关于托管内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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