调试.NET内存泄漏 - 如何知道什么是抱着什么参考? [英] Debugging .NET memory leaks - how to know what is holding a reference to what?

查看:132
本文介绍了调试.NET内存泄漏 - 如何知道什么是抱着什么参考?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在那里似乎是内存泄漏.NET应用程序。我知道教科书的答案,那应该事件退订,弃对象应释放等等...

I am working on a .NET application where there appears to be a memory leak. I know the text-book answers, that events should be unsubscribed, disposable objects should be disposed etc...

我有一个测试工具,能够重现错误。在某一类的终结我写安慰

I have a test harness that can reproduce the error. In the finalizer of a certain class I write to console

public class Foo
{
   // Ctor
   public Foo()
   {
   }

   ~public Foo()
   {
       Console.WriteLine("Foo Finalized");
   }
}

在测试工具,我创建一个单一实例美孚(这反过来又创造并与数百其他类型的相互作用),那么删除它并调用垃圾收集器。

In the test harness, I am creating a single instance of Foo (which in turn creates and interacts with hundreds of other types) then removing it and invoking the Garbage collector.

我发现了富终结永远不会被调用。我有这个设置,其中定稿作为对照试验类似的类。

I am finding the Foo Finalizer is never called. I have a similar class with this setup which is finalized as a control test.

所以我的问题是这样的:

So my question is this:

如何确定使用商用或开源工具到底是什么
持有至富的参考?

How can I determine using commercial or open source tools exactly what is holding a reference to Foo?

我公司有专业执照dotTrace内存分析器却可以'T从帮助文件如何使用它搞清楚。

I have a professional license to dotTrace Memory profiler but can't figure out from the help files how to use it.

更新:我现在用的 dotMemory 4.0 ,这是后继(好,但不可用)dotTrace内存3.5。

Update: I am now using dotMemory 4.0, which is the successor to the (good, but unusable) dotTrace Memory 3.5.

推荐答案

终结不确定性调用,所以用它来追踪以可靠的方式事情要小心了。如果删除终结,而是使用的WeakReference<富> ,你应该能够确定对象是否收集

The finalizer isn't deterministically called, so beware of using it to track things in a reliable way. If you remove the finalizer and instead use a WeakReference<Foo> you should be able to determine whether the object was collected.

所有存储器廓应该能够发现的问题,例如这一点,但具有不同的难度。我曾亲自使用蚂蚁,这是非常容易的哟使用,但不是免费的。它会帮助你展示的参考图的富实例,从GC根对象的所有道路。见此图它通常是容易被发现是谁在持有的参考。

All memory profilers should be able to find an issue such as this, but with varying degree of difficulty. I have personally used ANTS which is very easy yo use, but not free. It will help you show a reference diagram to the Foo instance, all the way from a GC root object. Seeing this diagram it is usually easy to spot who is holding the reference.

这篇关于调试.NET内存泄漏 - 如何知道什么是抱着什么参考?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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