C# sizeof() 等价于引用类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26570/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 08:11:47  来源:igfitidea点击:

sizeof() equivalent for reference types?

提问by John Sheehan

I'm looking for a way to get the size of an instance of a reference type. sizeof is only for value types. Is this possible?

我正在寻找一种获取引用类型实例大小的方法。sizeof 仅适用于值类型。这可能吗?

采纳答案by juan

If you don't mind it being a little less accurate than perfect, and for comparative purposes, you could serialize the object/s and measure that (in bytes for example)

如果您不介意它比完美更不准确,并且出于比较目的,您可以序列化对象并对其进行测量(例如以字节为单位)

EDIT (I kept thinking after posting): Because it's a little more complicated than sizeof for valuetypes, for example: reference types can have references to other objects and so on... there's not an exact and easy way to do it that I know of...

编辑(我在发布后一直在想):因为它比值类型的 sizeof 稍微复杂一点,例如:引用类型可以引用其他对象等等......我知道没有一种精确而简单的方法来做到这一点的...

回答by Greg Hurlman

You need Marshal.SizeOf

你需要Marshal.SizeOf

Edit:This isfor unsafe code, but then, so is sizeof().

编辑:针对不安全代码的,但是 sizeof() 也是如此。

回答by juan

Beware that Marshal.SizeOf is for unsafe code...

请注意 Marshal.SizeOf 用于不安全代码......

I don't think it's possible for managed code though, maybe you can explain your problem, there may be another way to solve it

我不认为托管代码是可能的,也许你可以解释你的问题,可能有另一种方法来解决它

回答by serhio

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

回答by GrantJ

I had a similar question recently and wanted to know the size of Object and LinkedListNode in C#. To solve the problem, I developed a programthat would:

我最近有一个类似的问题,想知道 C# 中 Object 和 LinkedListNode 的大小。为了解决这个问题,我开发了一个程序

  1. Measure the program's "Working Set"
  2. Allocate a lot of objects.
  3. Measure the "Working Set" again.
  4. Divide the difference by the number of allocated objects.
  1. 测量程序的“工作集”
  2. 分配很多对象。
  3. 再次测量“工作集”。
  4. 将差值除以分配的对象数。

On my computer (64-bit), I got the following data:

在我的计算机(64 位)上,我得到了以下数据:

Measuring Object:
iter    working set     size estimate
-1      11190272
1000000 85995520        74.805248
2000000 159186944       73.998336
3000000 231473152       73.4276266666667
4000000 306401280       73.802752
5000000 379092992       73.580544
6000000 451387392       73.3661866666667
7000000 524378112       73.3125485714286
8000000 600096768       73.613312
9000000 676405248       73.9127751111111
Average size: 73.7577032239859
Measuring LinkedListNode<Object>:
iter    working set     size estimate
-1      34168832
1000000 147959808       113.790976
2000000 268963840       117.397504
3000000 387796992       117.876053333333
4000000 507973632       118.4512
5000000 628379648       118.8421632
6000000 748834816       119.110997333333
7000000 869265408       119.299510857143
8000000 993509376       119.917568
9000000 1114038272      119.985493333333
Average size: 118.296829561905
Estimated Object size: 29.218576886067
Estimated LinkedListNode<reference type> size: 44.5391263379189

Based on the data, the average size of allocating millions of Objects is approximately 29.2 bytes. A LinkedListNode object is approximately 44.5 bytes. This data illustrates two things:

根据数据,分配数百万个对象的平均大小约为 29.2 字节。LinkedListNode 对象大约为 44.5 字节。这个数据说明了两件事:

  1. It's very unlikely that the system is allocating a partial byte. The fractional measure of bytes indicates the overhead the CLR requires to allocate and track millions of reference types.
  2. If we simply round-down the number of bytes, we're still unlikely to have the proper byte count for reference types. This is clear from the measure of Objects. If we round down, we assume the size is 29 bytes which, while theoretically possible, is unlikely because of padding. In order to improve performance, object allocations are usually padded for alignment purposes. I would guess that CLR objects will be 4 byte aligned.
  1. 系统不太可能分配部分字节。字节的小数度量表示 CLR 分配和跟踪数百万个引用类型所需的开销。
  2. 如果我们简单地舍入字节数,我们仍然不太可能为引用类型获得正确的字节数。这从对象的度量中可以清楚地看出。如果我们向下取整,我们假设大小为 29 字节,虽然理论上可能,但由于填充的原因不太可能。为了提高性能,通常为了对齐而填充对象分配。我猜想 CLR 对象将是 4 字节对齐的。

Assuming CLR overhead and 4-byte alignment, I'd estimate an Object in C# is 28 bytes and a LinkedListNode is 44 bytes.

假设 CLR 开销和 4 字节对齐,我估计 C# 中的对象是 28 字节,而 LinkedListNode 是 44 字节。

BTW Jon Skeet had the idea for the method above before I did and stated it in this answerto a similar question.

顺便说一句,Jon Skeet 在我之前就有了上述方法的想法,并在这个对类似问题的回答中说明了这一点

回答by Gomes

Please refer my answer in the below link.

请在下面的链接中参考我的回答。

It is possible via .sos.dll debugger extension

可以通过 .sos.dll 调试器扩展

Find out the size of a .net object

找出 .net 对象的大小