是否有可能在管理code内存泄漏? (特别是C#3.0)

问题描述:

举例来说,如果我有一个分层的数据结构:

For instance if I have a hierarchical data structure:

class Node
{
    public List<Node> children;
}

和它填充到多层次下来,然后在父母一方去:

and it is populated to many levels down then in one of the parents go:

myNode.children.Clear();

这将清除所有引用眼前的孩子们 - 但如何对所有的孙儿,盛大隆重的儿童等的被那些直接子引用?是C#足够聪明的知道自己不再需要的时候,就会被垃圾回收?

which will clear all the references to the immediate children - but how about all the grand children, grand grand children etc. that were referenced by those immediate children? Is C# clever enough to know they are no longer needed and they will be garbage collected?

我已经使用WPF数据,而无需实现接口INotifyChanged可能导致内存泄漏结合阅读:http://blogs.msdn.com/b/micmcd/archive/2008/03/07/avoiding-a-wpf-memory-leak-with-databinding-black-magic.aspx,怎么可能在托管环境?

I have read using WPF data binding without implementing interface INotifyChanged can cause memory leaks: http://blogs.msdn.com/b/micmcd/archive/2008/03/07/avoiding-a-wpf-memory-leak-with-databinding-black-magic.aspx, how is that possible in a managed environment?

是的,垃圾收集器将制定出孙子等都是垃圾。基本上,如果没有办法让一个物体,它被认为垃圾,符合回收。

Yes, the garbage collector will work out that the grandchildren etc are garbage. Basically, if there's no way of getting to an object, it's considered garbage and eligible for collection.

至于如何记忆泄漏是可能的管理code - 这是典型的,如果你最终与其中的的对象的访问通过对象引用,但那里是没有办法,你可以结束了通过API清算的引用。

As for how memory "leaks" are possible in managed code - it's typically if you end up with an object which is reachable via object references, but where there's no way you can end up "clearing" those references via an API.

这是在你所引用的博客文章的情况:

That's the case in the blog post you quoted:

有一个问题,WPF检查发现,实施INotifyProperyChanged事情。如果有一个数据绑定的东西没有实现此接口,那么它使一个全球性的表中的记录。这个纪录并没有得到清理,因为WPF已经没有办法时,不再需要一个数据库记录检查。

There is an issue where WPF checks to find things that implement INotifyProperyChanged. If there is a databinding to something not implementing this interface, then it makes a record in a global table. That record doesn't get cleaned up, as WPF has no way of checking when that DB record is no longer needed.

所以这是这个全球性的表,维持引用,你有没有办法显示,在表中的项目可以被清除的。

So there's this global table maintaining references, and you have no way of indicating that an item in the table can be cleared.