在运行时查找对对象的引用 [英] Find references to the object in runtime

查看:21
本文介绍了在运行时查找对对象的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象,它永远存在.我正在删除所有我能看到的引用,在使用它之后,但它仍然没有被收集.它的生命周期非常复杂,所以我不能确定所有的引用都被清除了.

I have an object, which lives forever. I am deleteing all references I can see, to it after using it, but it still not collected. Its life cycle is pretty sophisticated so I can't be sure that all references been cleared.

if ( container.Controls.Count > 0 )
{ 
    var controls = new Control[ container.Controls.Count ];
    container.Controls.CopyTo( controls, 0 );

    foreach ( var control in controls ) 
    { 
         container.Controls.Remove( control );
         control.Dispose();
    }

    controls = null; 
}

GC.Collect();
GC.Collect(1);
GC.Collect(2);
GC.Collect(3);

我怎样才能找出它仍然有哪些参考资料?为什么不收集?

How can I find out what references does it still have? Why is it not collected?

推荐答案

尝试使用 内存分析器,(例如 ants) 它会告诉你是什么让对象保持活力.尝试第二次猜测这类问题非常困难.

Try using a memory profiler, (e.g. ants) it will tell you what is keeping the object alive. Trying to 2nd guess this type of problem is very hard.

Red-gate 提供 14 天的试用期,应该足够多的时间来解决这个问题并确定内存分析器是否为您提供长期价值.

Red-gate gives 14 days trial that should be more then enough time to tack down this problem and decide if a memory profiler provides you with long term value.

市场上有 很多 其他内存分析器(例如 .NET 内存分析器) 他们中的大多数都有免费试用,但是我发现 Red-Gate 工具很容易使用,所以倾向于先尝试一下.

There are lots of other memory profilers on the market (e.g. .NET Memory Profiler) most of them have free trials, however I have found that the Red-Gate tools are easy to use, so tend try them first.

这篇关于在运行时查找对对象的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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