简体   繁体   English

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

[英]Find out how much memory is being used by an object in C#?

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 (eg a List that keeps track of something). 问的原因是,虽然C#内置了内存管理,但我常常遇到的问题是没有清除对象的实例(例如跟踪某些内容的List)。

There are couple of reasonably good memory profilers (eg ANTS Profiler) but in a multi-threaded environment is pretty hard to figure out what belongs where, even with those tools. 有几个相当不错的内存分析器(例如ANTS Profiler),但在多线程环境中很难弄清楚哪些属于哪里,即使使用这些工具。

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). 如果您没有尝试在代码本身中执行此操作,我假设基于您的ANTS参考,请尝试查看CLRProfiler(目前为v2.0)。 It's free and if you don't mind the rather simplistic UI, it can provide valuable information. 它是免费的,如果您不介意相当简单的UI,它可以提供有价值的信息。 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. 我曾经用它作为寻找记忆韭菜的一种工具。

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. 如果您确实希望在代码中执行此操作,则CLR具有您可以使用的分析API。 If you find the information in CLRProfiler, since it uses those APIs, you should be able to do it in code too. 如果您在CLRProfiler中找到信息,因为它使用这些API,您也应该能够在代码中执行此操作。 More info here: http://msdn.microsoft.com/de-de/magazine/cc300553(en-us).aspx 更多信息: 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。)

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! 快乐的剖析!

I have good experiences with MemProfiler . 我对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. 它为您提供了创建对象时的堆栈跟踪以及为什么对象仍未进行垃圾回收的所有图形。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM