查找引用在运行时对象对象

2023-09-03 00:27:43 作者:落花叩卷

我有一个对象,它是永远长存。我delete一个所有引用我所看到的,它用了之后,但它仍然没有收集。它的生命周期是pretty的复杂,所以我不能确保所有的引用被清除。

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?

推荐答案

尝试使用内存分析器,(如的蚂蚁的),它会告诉你什么是保持对象存活。试图第二猜测这种类型的问题是非常困难的。

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 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内存分析器 )大部分都有免费试用,但我发现,红门的工具很容易使用,所以倾向于尝试一下先。

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.