找出 C# 中的对象使用了多少内存?
有谁知道如何找出对象实例占用了多少内存?
例如,如果我有以下对象的实例:
TestClass tc = new TestClass();
有没有办法找出实例 tc 占用了多少内存?
询问的原因是,虽然 C# 内置了内存管理,但我经常遇到不清除对象实例的问题(例如,跟踪某些内容的列表)。
有一些相当不错的内存分析器(例如 ANTS Profiler),但在多线程环境中,即使使用这些工具,也很难弄清楚什么属于哪里。
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?
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).
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
CLR 分析器,由微软免费提供,在这类事情上做得非常好。
可以下载整个探查器的介绍 这里。 还有图案和图案 实践团队不久前整理了一些内容,详细介绍了如何使用探查器。
它在向您显示不同的线程和在这些线程中创建的对象方面做得相当合理。
希望这能带来一些启发。 快乐的分析!
The CLR Profiler, which is provide free by Microsoft does a very good job at this type of thing.
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!
我在 MemProfiler 方面拥有良好的体验。 它为您提供了对象创建时间的堆栈跟踪以及该对象仍未被垃圾收集的原因的所有图表。
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.
如果您不想在代码本身中执行此操作(我根据您的 ANTS 参考假设),请尝试查看 CLRProfiler(当前为 v2.0)。 它是免费的,如果您不介意相当简单的用户界面,它可以提供有价值的信息。 它将为您提供各种统计数据的深入概述。 不久前我用它作为寻找记忆韭菜的工具。
在此下载:https://github.com/MicrosoftArchive/clrprofiler
如果您确实想在代码,CLR 具有您可以使用的分析 API。 如果您在 CLRProfiler 中找到信息,因为它使用这些 API,您也应该能够在代码中执行此操作。 更多信息请点击这里:
http://msdn.microsoft.com/de-de /magazine/cc300553(en-us).aspx
(它不像使用 WinDbg 那样神秘,但要做好深入 CLR 的准备。)
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.
Download here: https://github.com/MicrosoftArchive/clrprofiler
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
(It's not as cryptic as using WinDbg, but be prepared to do mighty deep into the CLR.)