找出C#中对象使用了多少内存?

时间:2022-01-05 21:17:36

Does anyone know of a way to find out how much memory an instance of an object is taking?

有没有人知道一种方法来找出一个对象的实例占用多少内存?

For example, if I have an instance of the following object:

例如,如果我有以下对象的实例:

TestClass tc = new TestClass();

Is there a way to find out how much memory the instance tc is taking?

有没有办法找出实例tc占用多少内存?

The reason for asking, is that although C# has built in memory management, I often run into issues with not clearing an instance of an object (e.g. a List that keeps track of something).

问的原因是,虽然C#内置了内存管理,但我经常遇到不清除对象实例的问题(例如跟踪某事物的List)。

There are couple of reasonably good memory profilers (e.g. ANTS Profiler) but in a multi-threaded environment is pretty hard to figure out what belongs where, even with those tools.

有几个相当不错的内存分析器(例如ANTS Profiler),但在多线程环境中很难弄清楚哪些属于哪里,即使使用这些工具。

3 个解决方案

#1


7  

If you are not trying to do it in code itself, which I'm assuming based on your ANTS reference, try taking a look at CLRProfiler (currently v2.0). It's free and if you don't mind the rather simplistic UI, it can provide valuable information. It will give you a in-depth overview of all kinds of stats. I used it a while back as one tool for finding a memory leek.

如果您不想在代码本身中进行此操作,我假设基于您的ANTS参考,请尝试查看CLRProfiler(目前为v2.0)。它是免费的,如果你不介意相当简单的用户界面,它可以提供有价值的信息。它将为您提供各种统计数据的深入概述。我用了一段时间作为查找记忆韭菜的工具。

Download here: http://www.microsoft.com/downloads/details.aspx?FamilyId=A362781C-3870-43BE-8926-862B40AA0CD0&displaylang=en

在此处下载:http://www.microsoft.com/downloads/details.aspx?FamilyId = A362781C-3870-43BE-8926-862B40AA0CD0&displaylang = en

If you do want to do it in code, the CLR has profiling APIs you could use. If you find the information in CLRProfiler, since it uses those APIs, you should be able to do it in code too. More info here: http://msdn.microsoft.com/de-de/magazine/cc300553(en-us).aspx

如果您确实希望在代码中执行此操作,则CLR具有您可以使用的分析API。如果您在CLRProfiler中找到信息,因为它使用这些API,您也应该能够在代码中执行此操作。更多信息:http://msdn.microsoft.com/de-de/magazine/cc300553(en-us).aspx

(It's not as cryptic as using WinDbg, but be prepared to do mighty deep into the CLR.)

(它不像使用WinDbg一样神秘,但要做好准备深入CLR。)

#2


3  

The CLR Profiler, which is provide free by Microsoft does a very good job at this type of thing.

由Microsoft免费提供的CLR Profiler在这类事情上表现非常出色。

An introduction to the whole profiler can be downloaded here. Also the Patterns & Practices team put something together a while back detailing how to use the profiler.

可在此处下载整个分析器的介绍。此外,模式与实践团队还将一些内容放在一起,详细介绍了如何使用分析器。

It does a fairly reasonable job at showing you the different threads and objects created in those threads.

它在向您展示在这些线程中创建的不同线程和对象方面做了相当合理的工作。

Hope this sheds some light. Happy profiling!

希望这会有所启发。快乐剖析!

#3


2  

I have good experiences with MemProfiler. It gives you stack traces of when the object was created and all the graphs of why the object is still not garbage collected.

我对MemProfiler有很好的经验。它为您提供了创建对象时的堆栈跟踪以及对象仍未进行垃圾回收的所有图形。

#1


7  

If you are not trying to do it in code itself, which I'm assuming based on your ANTS reference, try taking a look at CLRProfiler (currently v2.0). It's free and if you don't mind the rather simplistic UI, it can provide valuable information. It will give you a in-depth overview of all kinds of stats. I used it a while back as one tool for finding a memory leek.

如果您不想在代码本身中进行此操作,我假设基于您的ANTS参考,请尝试查看CLRProfiler(目前为v2.0)。它是免费的,如果你不介意相当简单的用户界面,它可以提供有价值的信息。它将为您提供各种统计数据的深入概述。我用了一段时间作为查找记忆韭菜的工具。

Download here: http://www.microsoft.com/downloads/details.aspx?FamilyId=A362781C-3870-43BE-8926-862B40AA0CD0&displaylang=en

在此处下载:http://www.microsoft.com/downloads/details.aspx?FamilyId = A362781C-3870-43BE-8926-862B40AA0CD0&displaylang = en

If you do want to do it in code, the CLR has profiling APIs you could use. If you find the information in CLRProfiler, since it uses those APIs, you should be able to do it in code too. More info here: http://msdn.microsoft.com/de-de/magazine/cc300553(en-us).aspx

如果您确实希望在代码中执行此操作,则CLR具有您可以使用的分析API。如果您在CLRProfiler中找到信息,因为它使用这些API,您也应该能够在代码中执行此操作。更多信息:http://msdn.microsoft.com/de-de/magazine/cc300553(en-us).aspx

(It's not as cryptic as using WinDbg, but be prepared to do mighty deep into the CLR.)

(它不像使用WinDbg一样神秘,但要做好准备深入CLR。)

#2


3  

The CLR Profiler, which is provide free by Microsoft does a very good job at this type of thing.

由Microsoft免费提供的CLR Profiler在这类事情上表现非常出色。

An introduction to the whole profiler can be downloaded here. Also the Patterns & Practices team put something together a while back detailing how to use the profiler.

可在此处下载整个分析器的介绍。此外,模式与实践团队还将一些内容放在一起,详细介绍了如何使用分析器。

It does a fairly reasonable job at showing you the different threads and objects created in those threads.

它在向您展示在这些线程中创建的不同线程和对象方面做了相当合理的工作。

Hope this sheds some light. Happy profiling!

希望这会有所启发。快乐剖析!

#3


2  

I have good experiences with MemProfiler. It gives you stack traces of when the object was created and all the graphs of why the object is still not garbage collected.

我对MemProfiler有很好的经验。它为您提供了创建对象时的堆栈跟踪以及对象仍未进行垃圾回收的所有图形。