如何计算Nullable 数据类型的大小[重复]

时间:2022-02-05 10:24:38

This question already has an answer here:

这个问题在这里已有答案:

Actually, i am willing to know that how much memory is being consumed by following datatypes

实际上,我愿意知道以下数据类型消耗了多少内存

int? = memory size?

诠释? =内存大小?

double? = memory size?

双? =内存大小?

bool? = memory size?

布尔? =内存大小?

Can anybody give me information about their storage or a method to calculate their size

任何人都可以向我提供有关其存储的信息或计算其大小的方法

3 个解决方案

#1


1  

Do you want to know the memmory consumption of e.g. a int? x? MSDN says:

你想知道例如memmory消费吗?一个int? X? MSDN说:

... The common language runtime assigns storage based on the characteristics of the platform on which your application is executing. In some circumstances it packs your declared elements as closely together as possible; in other cases it aligns their memory addresses to natural hardware boundaries. Also, storage assignment is different on a 64-bit platform than it is on a 32-bit platform.

...公共语言运行库根据应用程序正在执行的平台的特征来分配存储。在某些情况下,它会将您声明的元素尽可能紧密地包装在一起;在其他情况下,它将其内存地址与自然硬件边界对齐。此外,64位平台上的存储分配与32位平台上的存储分配不同。

The same considerations apply to each member of a composite data type such as a structure or an array. Furthermore, some composite types have additional memory requirements. For example, an array uses extra memory for the array itself and also for each dimension. On a 32-bit platform, this overhead is currently 12 bytes plus 8 bytes for each dimension. On a 64-bit platform the requirement is doubled. You cannot rely on simply adding together the nominal storage allocations of the components.

相同的注意事项适用于复合数据类型的每个成员,例如结构或数组。此外,某些复合类型具有额外的内存要求。例如,数组为数组本身以及每个维度使用额外的内存。在32位平台上,此开销目前为每个维度12个字节加8个字节。在64位平台上,需求增加了一倍。您不能简单地将组件的标称存储分配简单地相加。

An Object referring to any elementary or composite data type uses 4 bytes in addition to the data contained in the data type.

引用任何基本或复合数据类型的对象除了包含在数据类型中的数据之外还使用4个字节。

#2


2  

answer, I believe, is here

我相信,答案就在这里

Basically, add to the size of the non-nullable the size of a bool.

基本上,添加一个bool大小的非可空的大小。

#3


1  

You can use the following code to get the actual size at runtime. The value returned will be the same as the element alignment of an array int?[], which is consistent with the value return by the CLI's sizeof opcode (ECMA-335 Partition I, §8.9.1). Since nullable types are treated as reference types, the C# sizeof operator cannot be used for this, even in an unsafe context. Instead, we use TypedReference and a 2-element array to calculate the same information.

您可以使用以下代码在运行时获取实际大小。返回的值将与数组int?[]的元素对齐相同,这与CLI的操作码size(ECMA-335分区I,§8.9.1)返回的值一致。由于可空类型被视为引用类型,因此即使在不安全的上下文中也不能使用C#sizeof运算符。相反,我们使用TypedReference和一个2元素数组来计算相同的信息。

public static int SizeOf<T>()
{
    T[] array = new T[2];
    TypedReference elem1 = __makeref(array[0]);
    TypedReference elem2 = __makeref(array[1]);

    unsafe
    {
        byte* address1 = (byte*)*(IntPtr*)(&elem1);
        byte* address2 = (byte*)*(IntPtr*)(&elem2);
        return (int)(address2 - address1);
    }
}

You can then use the following.

然后,您可以使用以下内容。

// This returns 8 on my test, but the runtime is free to change this to
// any value greater than sizeof(int)+sizeof(bool)
int nullableSize = sizeof(int?);

#1


1  

Do you want to know the memmory consumption of e.g. a int? x? MSDN says:

你想知道例如memmory消费吗?一个int? X? MSDN说:

... The common language runtime assigns storage based on the characteristics of the platform on which your application is executing. In some circumstances it packs your declared elements as closely together as possible; in other cases it aligns their memory addresses to natural hardware boundaries. Also, storage assignment is different on a 64-bit platform than it is on a 32-bit platform.

...公共语言运行库根据应用程序正在执行的平台的特征来分配存储。在某些情况下,它会将您声明的元素尽可能紧密地包装在一起;在其他情况下,它将其内存地址与自然硬件边界对齐。此外,64位平台上的存储分配与32位平台上的存储分配不同。

The same considerations apply to each member of a composite data type such as a structure or an array. Furthermore, some composite types have additional memory requirements. For example, an array uses extra memory for the array itself and also for each dimension. On a 32-bit platform, this overhead is currently 12 bytes plus 8 bytes for each dimension. On a 64-bit platform the requirement is doubled. You cannot rely on simply adding together the nominal storage allocations of the components.

相同的注意事项适用于复合数据类型的每个成员,例如结构或数组。此外,某些复合类型具有额外的内存要求。例如,数组为数组本身以及每个维度使用额外的内存。在32位平台上,此开销目前为每个维度12个字节加8个字节。在64位平台上,需求增加了一倍。您不能简单地将组件的标称存储分配简单地相加。

An Object referring to any elementary or composite data type uses 4 bytes in addition to the data contained in the data type.

引用任何基本或复合数据类型的对象除了包含在数据类型中的数据之外还使用4个字节。

#2


2  

answer, I believe, is here

我相信,答案就在这里

Basically, add to the size of the non-nullable the size of a bool.

基本上,添加一个bool大小的非可空的大小。

#3


1  

You can use the following code to get the actual size at runtime. The value returned will be the same as the element alignment of an array int?[], which is consistent with the value return by the CLI's sizeof opcode (ECMA-335 Partition I, §8.9.1). Since nullable types are treated as reference types, the C# sizeof operator cannot be used for this, even in an unsafe context. Instead, we use TypedReference and a 2-element array to calculate the same information.

您可以使用以下代码在运行时获取实际大小。返回的值将与数组int?[]的元素对齐相同,这与CLI的操作码size(ECMA-335分区I,§8.9.1)返回的值一致。由于可空类型被视为引用类型,因此即使在不安全的上下文中也不能使用C#sizeof运算符。相反,我们使用TypedReference和一个2元素数组来计算相同的信息。

public static int SizeOf<T>()
{
    T[] array = new T[2];
    TypedReference elem1 = __makeref(array[0]);
    TypedReference elem2 = __makeref(array[1]);

    unsafe
    {
        byte* address1 = (byte*)*(IntPtr*)(&elem1);
        byte* address2 = (byte*)*(IntPtr*)(&elem2);
        return (int)(address2 - address1);
    }
}

You can then use the following.

然后,您可以使用以下内容。

// This returns 8 on my test, but the runtime is free to change this to
// any value greater than sizeof(int)+sizeof(bool)
int nullableSize = sizeof(int?);