如何测量单个对象在.NET中占用的内存量

时间:2021-09-11 17:00:17

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?

我想知道在C#/ .NET和/或Visual Studio中是否有一个简单的命令或指令可以告诉我单个对象占用了多少内存?我有一种潜在的怀疑,即sizeof()操作符会骗我...我是否在这个信念中证明了这一点?

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

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

4 个解决方案

#1


8  

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:

还有很多问题也提到了这个问题......快速搜索出现了:

How to get object size in memory?

如何在内存中获取对象大小?

C#: Memory usage of an object

C#:对象的内存使用情况

Find out the size of a .net object

找出.net对象的大小

How much memory does a C#/.NET object use?

C#/ .NET对象使用多少内存?

and the list goes on an on...

这个清单继续......

#2


3  

There's no easy way and sizeof will only be good for value types. A typical object contains references to lists and other objects, so you would need to traverse all pointers in order to get the actual byte count, and add the pointer sizes as well.

没有简单的方法,sizeof只会对价值类型有利。典型对象包含对列表和其他对象的引用,因此您需要遍历所有指针以获取实际字节数,并添加指针大小。

You can check out the .Net Profiling API, or use a memory profiler like dotTrace. A memory profiler will at least help you to see where memory is allocated and if memory allocation is an issue in your application. This is often more useful than the actual object size.

您可以查看.Net Profiling API,或使用像dotTrace这样的内存分析器。内存分析器至少可以帮助您查看内存的分配位置以及内存分配是否是应用程序中的问题。这通常比实际对象大小更有用。

#3


3  

If you can - Serialize it!

如果你能 - 序列化它!

Dim myObjectSize As Long

Dim ms As New IO.MemoryStream
Dim bf As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
bf.Serialize(ms, myObject)
myObjectSize = ms.Position

#4


1  

I wonder how System.Runtime.InteropServices.Marshal.SizeOf() works? There are a lot of interesting static functions under the Marshal object that might be helpful here.

我想知道System.Runtime.InteropServices.Marshal.SizeOf()是如何工作的? Marshal对象下有许多有趣的静态函数可能对此有帮助。

#1


8  

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:

还有很多问题也提到了这个问题......快速搜索出现了:

How to get object size in memory?

如何在内存中获取对象大小?

C#: Memory usage of an object

C#:对象的内存使用情况

Find out the size of a .net object

找出.net对象的大小

How much memory does a C#/.NET object use?

C#/ .NET对象使用多少内存?

and the list goes on an on...

这个清单继续......

#2


3  

There's no easy way and sizeof will only be good for value types. A typical object contains references to lists and other objects, so you would need to traverse all pointers in order to get the actual byte count, and add the pointer sizes as well.

没有简单的方法,sizeof只会对价值类型有利。典型对象包含对列表和其他对象的引用,因此您需要遍历所有指针以获取实际字节数,并添加指针大小。

You can check out the .Net Profiling API, or use a memory profiler like dotTrace. A memory profiler will at least help you to see where memory is allocated and if memory allocation is an issue in your application. This is often more useful than the actual object size.

您可以查看.Net Profiling API,或使用像dotTrace这样的内存分析器。内存分析器至少可以帮助您查看内存的分配位置以及内存分配是否是应用程序中的问题。这通常比实际对象大小更有用。

#3


3  

If you can - Serialize it!

如果你能 - 序列化它!

Dim myObjectSize As Long

Dim ms As New IO.MemoryStream
Dim bf As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
bf.Serialize(ms, myObject)
myObjectSize = ms.Position

#4


1  

I wonder how System.Runtime.InteropServices.Marshal.SizeOf() works? There are a lot of interesting static functions under the Marshal object that might be helpful here.

我想知道System.Runtime.InteropServices.Marshal.SizeOf()是如何工作的? Marshal对象下有许多有趣的静态函数可能对此有帮助。