找出 C# 中的对象使用了多少内存?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/60820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find out how much memory is being used by an object in C#?
提问by user6301
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# 内置了内存管理,但我经常遇到不清除对象实例的问题(例如,跟踪某些内容的列表)。
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),但在多线程环境中,即使使用这些工具,也很难弄清楚什么属于哪里。
回答by Lars Truijens
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有很好的经验。它为您提供对象创建时间的堆栈跟踪,以及为什么对象仍未被垃圾回收的所有图表。
回答by Alex Duggleby
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)。它是免费的,如果您不介意相当简单的 UI,它可以提供有价值的信息。它将为您提供各种统计数据的深入概述。不久前,我将它用作寻找记忆韭菜的一种工具。
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。)
回答by Scott Saad
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 somethingtogether 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!
希望这会有所启发。快乐的剖析!