views:

174

answers:

2

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. :-)

A: 

The maximum size of any one object in .NET is 2GB.

This, of course, puts a hard cap on how large you can make a raw array.

You can make an "array of arrays" (and even create your own indexer to access them as though it was one contiguous array) pretty much as large as you like if you define your own class for it.

Anon.
Why then did Microsoft see fit to add LongLength to the array object in .NET 2.0?
Keith Hill
@Keith: I suspect that was there for future-proofing, although it's not really usable in any meaningful way in the current CLR implementations (including CLR 4).
Reed Copsey
@Keith: "Why did Microsoft add LongLength?" - I'd guess that it's because the 2GB object size limit is an implementation detail, not a contracted limit. It might also be that at some point they planned to up the limit, but found that the benefits weren't worth the problems they ran into (or something along those lines).
Michael Burr
+4  A: 

An array could theoretically have at most 2,147,483,647 elements, since it uses an int for indexing.

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.

Reed Copsey