获取一个字段的大小与C#字节 [英] Getting the size of a field in bytes with C#

查看:142
本文介绍了获取一个字段的大小与C#字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我要检查它的领域一类,并报告多字节最终如何做每场起飞。我承担所有字段是类型作为的Int32,字节等。
我如何才能找到轻松多少字节确实实地走?

I'm having a class which I want to inspect it's fields, and report eventually how much bytes does each field take. I assume all fields are of types as Int32, byte etc. How can I find out easily how much bytes does the field take?

我需要的是这样的:


INT32一个;
// INT a_size = a.GetSizeInBytes;
// a_size应为4

推荐答案

您不能,基本上是这样。这将取决于填充,这很可能会根据您正在使用的CLR版本和处理器等,这是比较容易制定出一个物体的总规模,假设它有对其他对象的引用:创建一个大阵,使用 GC.GetTotalMemory 一个基点,以新的引用填充数组你的类型的实例,然后再打电话GetTotalMemory。取一个值远离另一,并且通过实例的数量除以。你或许应该事先创建一个实例,以确保没有新​​的即时编译code有助于号码。是的,这是因为哈克因为它的声音 - 但我以前现在使用,效果良好。

You can't, basically. It will depend on padding, which may well be based on the CLR version you're using and the processor etc. It's easier to work out the total size of an object, assuming it has no references to other objects: create a big array, use GC.GetTotalMemory for a base point, fill the array with references to new instances of your type, and then call GetTotalMemory again. Take one value away from the other, and divide by the number of instances. You should probably create a single instance beforehand to make sure that no new JITted code contributes to the number. Yes, it's as hacky as it sounds - but I've used it to good effect before now.

就在昨天,我在想这将是一个好主意,写一个小的辅助类此。让我知道如果你有兴趣。

Just yesterday I was thinking it would be a good idea to write a little helper class for this. Let me know if you'd be interested.

编辑:有两个其他的建议,我想解决他们两个

There are two other suggestions, and I'd like to address them both.

首先,的sizeof操作符:这只能说明该类型的多少空间占用抽象的,没有浸轧圆了。 (它包括一个结构内填充,但不是填充应用到另一种类型的内该类型的变量。)

Firstly, the sizeof operator: this only shows how much space the type takes up in the abstract, with no padding applied round it. (It includes padding within a structure, but not padding applied to a variable of that type within another type.)

接下来, Marshal.SizeOf :这只能说明编组后,非托管的大小,而不是实际大小在存储器中。由于文件明确规定:

Next, Marshal.SizeOf: this only shows the unmanaged size after marshalling, not the actual size in memory. As the documentation explicitly states:

返回的尺寸是实际的
  非托管类型的大小。该
  的非托管和托管规模
  对象可以不同。对于字符
  类型,大小受
  适用于类的字符集。

The size returned is the actually the size of the unmanaged type. The unmanaged and managed sizes of an object can differ. For character types, the size is affected by the CharSet value applied to that class.

和再次填充可以有所作为。

And again, padding can make a difference.

只是为了澄清我的意思大约正在填充相关的,考虑这两个类:

Just to clarify what I mean about padding being relevant, consider these two classes:

class FourBytes { byte a, b, c, d; }
class FiveBytes { byte a, b, c, d, e; }

在我的x86机器,FourBytes的一个实例,需要12个字节(包括间接费用)。 FiveBytes的实例需要16个字节。唯一不同的是电子变量 - 这是否占用4个字节?嗯,有点......和排序的不是。显然相当,您可以从FiveBytes消除任何单变量获得规模回落到12个字节,但是这并不意味着的的变量每个的占用4个字节(想想去除所有的人!)。一个变量的成本只是不是一个概念,这使得有很大的意义在这里。

On my x86 box, an instance of FourBytes takes 12 bytes (including overhead). An instance of FiveBytes takes 16 bytes. The only difference is the "e" variable - so does that take 4 bytes? Well, sort of... and sort of not. Fairly obviously, you could remove any single variable from FiveBytes to get the size back down to 12 bytes, but that doesn't mean that each of the variables takes up 4 bytes (think about removing all of them!). The cost of a single variable just isn't a concept which makes a lot of sense here.

这篇关于获取一个字段的大小与C#字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆