Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maximum size of one- and twodimensional arrays in the CLR

Tags:

arrays

c#

.net

clr

When creating a long[] in C#, due to the size limitation of 2GiB for any object in the CLR, I expect it to be able to hold a maximum of 2GiB / 64 Bit = 268,435,456 Elements. However, the maximum number of elements that the array can actually hold before throwing an exception is 268,435,448. Also, a long[][] can hold multiple long[]s with the above number of elements, thus being substantially larger than 2GiB. My questions are:

  • Where did those 64 bytes go that cannot be allocated. What are they used for by the CLR?
  • Why can a twodimensional array be larger than 2GiB?

1 Answers

Where did those 64 bytes go that cannot be allocated. What are they used for by the CLR?

Part of them go for the object header (sync block and vtable pointer, two pointers) and part for the array dimensions. Also, possibly a few pointers are used by the managed heap itself, because an object that big will require a separate chunk of heap.

Why can a twodimensional array be larger than 2GiB?

Because it is not a single CLR object. Every inner array is a separate object limited to 2GB, and the outer array only holds references to the inner arrays.

like image 122
Anton Tykhyy Avatar answered Sep 04 '25 18:09

Anton Tykhyy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!