Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the maximum length of an array in .NET on 64-bit Windows

Tags:

arrays

.net

max

I heard from someone that the maximum array size in .NET is 4 GB? Just wondering if that is true. You wouldn't dream of doing this on 32-bit .NET but on a 64-bit system with 12 GB of RAM, maybe, just maybe you might want to do this. :-)

like image 781
Keith Hill Avatar asked Feb 26 '10 00:02

Keith Hill


People also ask

What is the maximum array size in C#?

By default, the maximum size of an Array is 2 gigabytes (GB).

What is the maximum possible size of an array?

The maximum allowable array size is 65,536 bytes (64K). Reduce the array size to 65,536 bytes or less. The size is calculated as (number of elements) * (size of each element in bytes).

What is the length size of array?

By definition, the length property of an array is an unsigned, 32-bit integer that is always numerically greater than the highest index in the array. The value of the length is 232. It means that an array can hold up to 4294967296 (232) elements.

What is the maximum size of an integer array?

Since the index of the array is int, the approximate index value can be 2^31 – 1. Based on this approximation, we can say that the array can theoretically hold 2,147,483,647 elements.


2 Answers

An array could theoretically have at most 2,147,483,647 elements, since it uses an int for indexing. The actual limit is slightly lower than this, depending on the type contained within the array.

However, there is a 2GB maximum single object restriction in the .NET CLR, even in 64bit. This was done by design.

You can easily make an IList<T> implementation that, internally, keeps multiple arrays, and allows you to grow beyond the 2GB single object limit, but there is not one in the framework itself.

Typically, however, this is not a real problem. Most of the time, you'll have arrays pointing to large classes - so the array is just holding references. This would mean your array can effectively point to many, many GBs of memory - but the array itself cannot be >2GB.


Note that, as of .NET 4.5, there is a new option available where 64bit applications can opt-in: gcAllowVeryLargeObjects. With this new option set, it is possible to get UInt32.MaxValue (4,294,967,295) elements in a multi-dimensional array, though a single dimensional array is still limited to 2,146,435,071 elements (2,147,483,591 for single byte arrays or arrays of a struct containing nothing ut a byte).

The new rules with this option are:

  • The maximum number of elements in an array is UInt32.MaxValue.
  • The maximum index in any single dimension is 2,147,483,591 (0x7FFFFFC7) for byte arrays and arrays of single-byte structures, and 2,146,435,071 (0X7FEFFFFF) for other types.
  • The maximum size for strings and other non-array objects is unchanged.
like image 193
Reed Copsey Avatar answered Oct 11 '22 10:10

Reed Copsey


In versions of .NET prior to 4.5, the maximum object size is 2GB. From 4.5 onwards you can allocate larger objects if gcAllowVeryLargeObjects is enabled. Note that the limit for string is not affected, but "arrays" should cover "lists" too, since lists are backed by arrays.

like image 27
Marc Gravell Avatar answered Oct 11 '22 11:10

Marc Gravell