如何衡量一个单独的对象需要在.NET中的内存量存量、对象、NET

2023-09-04 01:10:55 作者:既然没有既然何必再说何必

我想知道如果有一个简单的命令或指令在C#/。NET和/或Visual Studio,可以告诉我一个单独的对象了多少内存占用?我有一个鬼鬼祟祟的怀疑,大小()运算符是要骗我...我是合理的,这种信念?

I'm wondering if there is a simple command or instruction in C#/.NET and/or Visual Studio that can tell me how much memory an individual object is taking up? I have a sneaking suspicion that the sizeof() operator is going to lie to me ... am I justified in this belief?

有一个有点相关的问题这里,但没有明确的答案给出的如何测量单个对象

There is a somewhat related question here, but no definitive answer is given on how to measure an individual object

推荐答案

有没有明确的方式,因为它只是任何类型的对象并不简单。

There is no definitive way because it's not simple for just any type of object.

如果该对象包含对其他对象?如果这些对象有其它对象引用它们?哪一个对象实际拥有的内存空间?它是在创建它的一个或最后一个去触摸它?在任何一点它可能具有不同的所有者。或者你只是关心多少空间参考需要?

What if that object contains references to other objects? What if those other objects have other objects referencing them? Which object actually owns that memory space? Is it the one that created it or the last one to touch it? At any one point it could have different owners. Or do you just care about how much space the reference takes?

也有一吨的问题已经问过这个,以及...一个快速搜索变成了:

There is also a ton of questions that have asked this as well... a quick search turns up:

http://stackoverflow.com/questions/605621/how-to-get-object-size-in-memory

http://stackoverflow.com/questions/555929/c-memory-usage-of-an-object

http://stackoverflow.com/questions/324053/find-out-the-size-of-a-net-object

http://stackoverflow.com/questions/426396/how-much-memory-does-a-c-net-object-use

和不胜枚举的上...

and the list goes on an on...