What is the memory footprint of a Nullable<T>

Deadly 提交于 2019-12-17 13:44:38

问题


An int (Int32) has a memory footprint of 4 bytes. But what is the memory footprint of:

int? i = null;

and :

int? i = 3;

Is this in general or type dependent?


回答1:


I'm not 100% sure, but I believe it should be 8 Bytes, 4 bytes for the int32, and (since every thing has to be 4-Byte aligned on a 32 bit machine) 4 bytes for a boolean indicating whether the integer value has been specified or not.

On a 64 Bit machine it would still be 8 bytes (64 bits) since that is the smallest chunk of memory that can be addressed...




回答2:


The size of Nullable<T> is definitely type dependent. The structure has two members

  • boolean: For the hasValue
  • value: for the underlying value

The size of the structure will typically map out to 4 plus the size of the type parameter T.




回答3:


            int? a = 3;
  00000038  lea         ecx,[ebp-48h] 
  0000003b  mov         edx,3 
  00000040  call        78BFD740 
  00000045  nop              
            a = null;
  00000046  lea         edi,[ebp-48h] 
  00000049  pxor        xmm0,xmm0 
  0000004d  movq        mmword ptr [edi],xmm0 

It seems that first dword is for the value, and the second one is for null flag. So, 8 bytes total.

Curious, BinaryWritter doesn't like to write nullable types. I was wandering if it could pack it tighter then 8 bytes...




回答4:


An int? is a struct containing a boolean hasValue, and an int. Therefore, it has a footprint of 5 bytes. The same applies to all instances of a nullable<T>: size = sizeof(T)+sizeof(bool)




回答5:


The nullable type is a structure that contains the regular variable and a flag for the null state.

For a nullable int that would mean that it contains five bytes of data, but it's of course padded up to complete words, so it's using eight bytes.

You can generally expect that any nullable type will be four bytes larger than the regular type, except for small types like byte and boolean.




回答6:


32-bit and 64-bit machines:

  • int == 4 bytes
  • int? == 8 bytes == 4 for int + 4 for the nullable type wrapper.

The nullable type wrapper requires 4 bytes of storage. And the integer itself requires 4 bytes for each element. This is an efficient implementation. In an array many nullable types are stored in contiguous memory.

Based on a personal test (.NET Framework 4.6.1, x64, Release) and from – https://www.dotnetperls.com/nullable-memory

Also, if interesting: why int on x64 equals only 4 bytes?

Note: this is valid for Nullable<int> only, the size of Nullable<T> totally depends on the type.




回答7:


You can check using some code similar to the one at https://www.dotnetperls.com/nullable-memory.

I got the following results:

  • Int32 4 bytes
  • Int32? 8 bytes
  • Int16 2 bytes
  • Int16? 4 bytes
  • Int64 8 bytes
  • Int64? 16 bytes
  • Byte 1 bytes
  • Byte? 2 bytes
  • bool 1 bytes
  • bool? 2 bytes


来源:https://stackoverflow.com/questions/1381308/what-is-the-memory-footprint-of-a-nullablet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!