我可以在C#中获取某种类型的活动对象的实例吗? [英] Can I get the instances of alive objects of a certain type in C#?

查看:123
本文介绍了我可以在C#中获取某种类型的活动对象的实例吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是C#3.0问题。我可以使用.net框架提供的反射或内存管理类来计算内存中某种类型的活动实例总数吗?

This is a C# 3.0 question. Can I use reflection or memory management classes provided by .net framework to count the total alive instances of a certain type in the memory?

我可以使用内存分析器,但是需要额外的时间来转储内存,并且需要第三方软件。我想要的只是监视某种类型,我想要一种轻量级的方法,可以轻松地进行单元测试。计算活动实例的目的是确保我没有任何预期的活动实例导致内存泄漏。

I can do the same thing using a memory profiler but that requires extra time to dump the memory and involves a third party software. What I want is only to monitor a certain type and I want a light-weighted method which can go easily to unit tests. The purpose to count the alive instances is to ensure I don't have any expected living instances that cause "memory leak".

谢谢。

推荐答案

要完全在应用程序中完成此操作,您可以执行实例计数器,但是需要在每个类中进行显式编码和管理-没有我知道的最重要的要点是,您可以从执行代码中查询框架,以查看有多少实例在运行。

To do it entirely within the application you could do an instance-counter, but it would need to be explicitly coded and managed inside each class--there's no silver bullet that I'm aware of to let you query the framework from within the executing code to see how many instances are alive.

您真正要的是探查器的域。您可以购买一个或自己构建一个,但它要求您的应用程序作为探查器的子进程运行。顺便说一下,自己滚动并不是一件容易的事。

What you're asking for is really the domain of a profiler. You can purchase one or build your own, but it requires your application to run as a child process of the profiler. Rolling your own isn't an easy undertaking, by the way.

如果要考虑实例计数器,它必须是这样的:

If you want to consider the instance counter it would have to be something like:

public class MyClass : IDisposable
    public MyClass()
    {
        ++ClassInstances;
    }

    public void Dispose()
    {
        --ClassInstances;
    }

    private static new object _ClassInstancesLock;
    private static int _ClassInstances;
    private static int ClassInstances
    {
        get
        {
            lock (_ClassInstancesLock)
            {
                return _ClassInstances
            }
        }
    }

这只是一个非常粗糙的示例,没有针对汇编; 0%保证线程安全(对于这种类型的方法很关键),这为调用Dispose(实例计数器减量,但对象未正确进行GC)敞开了大门。要诊断出您需要的那种喜悦,您猜到了,它是专业的探查器,或者至少是windbg。

编辑:我刚刚注意到您问题的最后一行,需要说的是,我的上述方法虽然次充好且容易出错,但几乎可以保证在您遇到的情况下欺骗您并向您说谎重新遇到泄漏。应对这些问题的最佳工具IMO是ANTS Memory Profiler。第5版具有双重优势,因为它们将Performance和Memory Profiler分为两个单独的SKU(以前捆绑在一起),但是Memory Profiler 5.0的速度非常快。剖析这些问题过去一直很缓慢,但它们已经解决了。

I just noticed the very last line of your question and needed to say that my above approach, as shoddy and failure-prone as it is, is almost guaranteed to deceive and lie to you about the true number of instances if you're experiencing a leak. The best tool, IMO, for attacking these problems is ANTS Memory Profiler. Version 5 is a double-edge in that they broke Performance and Memory profiler into two seperate SKUs (used to be bundled together) but Memory Profiler 5.0 is absolutely lightning fast. Profiling these problems used to be slow as molases, but they've gotten around it somehome.

除非这是针对意图重新分配为0的个人项目,否则应该投资ANTS需要几百美元-但一定要先使用它的试用期。这是精确这种分析的好工具。

Unless this is for a personal project with 0 intent of redistribution you should invest the few hundred dollars needed for ANTS--but by all means use it's trial period first. It's a great tool for exactly this kind of analysis.

这篇关于我可以在C#中获取某种类型的活动对象的实例吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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