tags:

views:

514

answers:

6

In C# 2008, what is the Maximum Size that an Array can hold?

+7  A: 
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.

Matthew Scharley
Complete side note, but whenever one asks a question like this it usually means someone is doing something wrong.
Rob
This is true, and I'll add some stuff about Lists now, but it's an interesting question, especially when you get into making your own indexable classes, you can use ie. `long` to provide more potential values, if that's an issue for you.
Matthew Scharley
I'm pretty sure that .NET also imposes a maximum object size of 2GB. This means that even if arrays used an `Int64` indexer, the max number of elements in a `byte[]` array would still be restricted to about `2^31`; The max number of elements in an `int[]` array would be about `(2^31)/4`; The max number of elements in a `long[]` array would be about `(2^31)/8` etc etc.
LukeH
Worthy of noting, if it does. I don't know one way or the other, but it strikes me as an implementation concern, one that might be lifted or changed as the amount of memory in computers increases. The limits I describe won't change, because they'd cause major changes in the way that the framework is composed. (ie, code that checks the max size of an array by using `Int32.MaxValue` will instantly be incorrect)
Matthew Scharley
The 2 GB limit is real. Please see this related question: http://stackoverflow.com/questions/1087982/single-objects-still-limited-to-2-gb-in-size-in-clr-4-0
Brian Rasmussen
A: 

You mean maximum length or maximum size for each element? Both are only limited by available memory.

Jeremybub
how many elements in an array?
DotNetRookie
A: 

I think it is linked with your RAM (or probably virtual memory) space and for the absolute maximum constrained to your OS version (e.g. 32 bit or 64 bit)

waqasahmed
Actually, it's limited by the indexing value (which is an `int`, or `Int32`). Then it is limited by RAM/VRAM if you can't hold that many.
Matthew Scharley
yes, I think I modified my ansewr just before you posted yours.
waqasahmed
+2  A: 

Here is an answer to your question that goes into detail: http://www.velocityreviews.com/forums/t372598-maximum-size-of-byte-array.html

You may want to mention which version of .NET you are using and your memory size.

You will be stuck to a 2G, for your application, limit though, so it depends on what is in your array.

James Black
A: 

Since Length is an int I'd say Int.MaxValue

Preet Sangha
A: 

I think if you don't consider the VM, it is Integer.MaxValue

Peter Lee