What is the memory footprint of a Nullable<T>
Asked Answered
S

9

27

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?

Sherri answered 4/9, 2009 at 20:8 Comment(1)
In any case, it doesn't matter whether you have a value in it, or it is "null". It will occupy the same number of bytes regardless.Stutz
B
18

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) another 4 bytes for a boolean indicating whether the integer value has been specified or not.

Note, thanks to @sensorSmith, I am now aware that newer releases of .Net allow nullable values to be stored in smaller footprints (when the hardware memory design allows smaller chunks of memory to be independently allocated). 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...

A nullable for example only requires a single bit for the boolean, and another single bit for the IsNull flag and so the total storage requirements is less than a byte it theoretically could be stored in a single byte, however, as usual, if the smallest chunk of memory that can be allocated is 8 bytes (like on a 64 bit machine), then it will still take 8 bytes of memory.

Buke answered 4/9, 2009 at 20:11 Comment(6)
A 64 bit machine has a 64 bit (8 byte) wide bus, so, as I understand it, every data fetch (or write) brings back 64 bits (8 bytes ) from memory.Buke
The size is different again once boxed, as Nullable<T> disappears so its either null, or a boxed int.County
The boolean is not 4 bytes. According to MSDN sizeof(bool) returns 1, so a Nullable<Int32> would take 5 bytes instead. I don't think padding is added to the end of structs.Nestle
@Virtlink true, but effectively the boolean takes 4 bytes due to memory alignment most of the time. For instance when having an array of Nullable<int>.Matos
sizeof gives an error, and recommends using System.Runtime.InteropServices.Marshal.SizeOf instead. But this returns 8 bytes for double? I then tried to write a double? value into a byte array, and it turns out that 16 bytes are used. Am I wrong, or is this extremely inefficient? 7 bytes for alignment?Statued
Not every thing has to be 4-Byte aligned on a 32-bit machine. See my answer.Appomattox
P
14

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.

Pete answered 4/9, 2009 at 20:15 Comment(0)
P
5
            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...

Phagocyte answered 4/9, 2009 at 20:55 Comment(3)
In a networking project at my former employer, we would encode it into 33 Bits into the data stream.Harrumph
33 bits doesn't cut nicely :)Airworthy
I know. We were employing bit-wise alignment, and sometimes even more advanced packing, like encoding 3 threestates (27 possibilities) into 5 bit.Harrumph
A
3

The .NET (and most other languages/frameworks) default behavior is to align struct fields to a multiple of their size and structs themselves to a multiple of the size of their largest field. Reference: StructLayout

Nullable<T> has a bool flag and the T value. Since bool takes just 1 byte, the size of the largest field is the size of T; and Nullable doubles the space needed compared to a T alone. Reference:Nullable Source

Clarification: If T is itself a non-primitive struct rather than a primitive type, Nullable increases the space needed by the size of the largest primitive field within T or, recursively, within any of T's non-primitive fields. So, the size of a Nullable<Nullable<bool>> is 3, not 4.

Appomattox answered 14/2, 2021 at 0:26 Comment(0)
M
2

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
Mccammon answered 28/2, 2019 at 6:11 Comment(0)
P
0

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)

Polymerism answered 4/9, 2009 at 20:11 Comment(1)
Good point. I don't know much about how data is packed together.Polymerism
S
0

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.

Soak answered 4/9, 2009 at 20:13 Comment(0)
S
0

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.

Stimulant answered 3/1, 2019 at 8:47 Comment(0)
C
0

On .NET Framework 4.8:

List<double?> list1 = new List<double?>();
for (int i=0; i<10000000; i++)
{
  list1.Add( 1 );
} 

list1 uses 262 165 KB

List<double?> list2 = new List<double?>();
for (int i=0; i<10000000; i++)
{
  list1.Add( 2 );
} 

list2 uses 131 093 KB

Cockle answered 9/5, 2023 at 15:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.