The Limitation on the Size of .Net Array

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

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.

The limitation on the size of .Net array

That is correct. No single object can be larger than 2 GB.

As with 32-bit Windows operating
systems, there is a 2GB limit on the
size of an object you can create while
running a 64-bit managed application
on a 64-bit Windows operating system.

This question has additional details and some useful links: Single objects still limited to 2 GB in size in CLR 4.0?

What is the Maximum Size that an Array can hold?

System.Int32.MaxValue

Assuming you mean System.Array, ie. any normally defined array (int[], etc). This is the maximum number of values the array can hold. The size of each value is only limited by the amount of memory or virtual memory available to hold them.

This limit is enforced because System.Array uses an Int32 as it's indexer, hence only valid values for an Int32 can be used. On top of this, only positive values (ie, >= 0) may be used. This means the absolute maximum upper bound on the size of an array is the absolute maximum upper bound on values for an Int32, which is available in Int32.MaxValue and is equivalent to 2^31, or roughly 2 billion.

On a completely different note, if you're worrying about this, it's likely you're using alot of data, either correctly or incorrectly. In this case, I'd look into using a List<T> instead of an array, so that you are only using as much memory as needed. Infact, I'd recommend using a List<T> or another of the generic collection types all the time. This means that only as much memory as you are actually using will be allocated, but you can use it like you would a normal array.

The other collection of note is Dictionary<int, T> which you can use like a normal array too, but will only be populated sparsely. For instance, in the following code, only one element will be created, instead of the 1000 that an array would create:

Dictionary<int, string> foo = new Dictionary<int, string>();
foo[1000] = "Hello world!";
Console.WriteLine(foo[1000]);

Using Dictionary also lets you control the type of the indexer, and allows you to use negative values. For the absolute maximal sized sparse array you could use a Dictionary<ulong, T>, which will provide more potential elements than you could possible think about.

Array declared with maximum size throws OutOfMemoryException in C#

The NET framework limits the maximum size of any object to 2 GB by default. Arrays can be bigger on 64-bit platforms if you enable the corresponding setting gcAllowVeryLargeObjects.

So theoretically, the limit of an int array should be around 536 870 912 elements, but that doesn't account for the memory footprint of the array itself, therefore the real limit must be less.

Another issue is that arrays need to be allocated in contiguous memory space. If the allocator can't find any such space, you will get an OutOfMemoryException even if the object is under the maximum size limit.

Max Array Size in .NET Core / .NET 5

The array size is limited to a total of 4 billion elements, and to a
maximum index of 0X7FEFFFFF in any given dimension (0X7FFFFFC7 for
byte arrays and arrays of single-byte structures).

Taken from Remarks

Maximum size of string array in C#

Array Class

By default, the maximum size of an Array is 2 gigabytes (GB). In a
64-bit environment, you can avoid the size restriction by setting the
enabled attribute of the gcAllowVeryLargeObjects
configuration element
to true in the run-time environment. However, the array will still be
limited to a total of 4 billion elements
, and to a maximum index of
0X7FEFFFFF in any given dimension (0X7FFFFFC7 for byte arrays and
arrays of single-byte structures).

Very useful comment by Ňuf

But is should be noted that strings themself do not count towards the
2GB size limit, because the array contains only references to these
strings. So the maximal number of elements in string array is approx.
500M in 32bit process and 2G in 64bit process. Also this limit only
applies to .NET CLR, other implementations may have different limits
(e.g. Mono on 64bit supports even larger arrays with
–enable-big-arrays option)

Maximum size of .NET arrays

According to SpankyJ, .NET 2.0 at least had a limit of 2 GB per array. 8 bytes (sizeof long) * ~2^31 = 16 gigabytes (not to mention the actual memory required). As for the Overflow, I agree with marcc that is probably because arrays are expected to be int-indexed (see e.g. this method Though I'm a bit uncertain regarding this, as this overload takes an array of Int64 lengths. This may be an extension only available in later versions.

Overall, though, this is mostly a theoretical issue. Anyone actually relying on this is likely doing something wrong.

Is the size of an array constrained by the upper limit of int (2147483647)?

Anytime you are working with an array this big, you should probably try to find a better solution to the problem. But that being said I'll still attempt to answer your question.

As mentioned in this article there is a 2 GB limit on any object in .Net. For all x86, x64 and IA64.

As with 32-bit Windows operating
systems, there is a 2GB limit on the
size of an object you can create while
running a 64-bit managed application
on a 64-bit Windows operating system.

Also if you define an array too big on the stack, you will have a stack overflow. If you define the array on the heap, it will try to allocate it all in one big continuous block. It would be better to use an ArrayList which has implicit dynamic allocation on the heap. This will not allow you to get past the 2GB, but will probably allow you to get closer to it.

I think the stack size limit will be bigger only if you are using an x64 or IA64 architecture and operating system. Using x64 or IA64 you will have 64-bit allocatable memory instead of 32-bit.

If you are not able to allocate the array list all at once, you can probably allocate it in parts.

Using an array list and adding 1 object at a time on an x64 Windows 2008 machine with 6GB of RAM, the most I can get the ArrayList to is size: 134217728. So I really think you have to find a better solution to your problem that does not use as much memory. Perhaps writing to a file instead of using RAM.

.NET object size limit

.NET limits any object to max 2 GB even on 64 bit platforms. You can create your own data type, that uses multiple objects to store more data, thus getting around the 2 GB limit of a single object. For instance a List<float[]> would allow you to store more than 2 GB, but you would have to write the necessary plumbing code to make it behave similar to a single, large array.

You may also want to check this question.



Related Topics



Leave a reply



Submit