tags:

views:

126

answers:

4

I heard that there is a hard limit on the size of .Net Array. It is said that the maximum amount of memory that can be allocated to any single instance of an Array object ( regardless of whether it's int[], double[] or your own array) is 2GB. And no, if you have a 64 bit machine, the 2GB limit is still there.

I'm not sure whether my impression is correct or not. Anyone can confirm?

+5  A: 

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: http://stackoverflow.com/questions/1087982/single-objects-still-limited-to-2-gb-in-size-in-clr-4-0

Brian Rasmussen
Is there also a limit on the number of elements in the array?
uriDium
@uriDium: effectively yes, since an array is one object.
Brian Rasmussen
This question has additional info on the size of arrays in .NET http://stackoverflow.com/questions/1589669/overhead-of-a-net-array/1589759#1589759
Brian Rasmussen
@Brian Hi, I meant not memory wise but how many elements an array can hold. But I suppose maybe the number of elements it can hold is restricted by memory first and not the fact that the index is an integer value?
uriDium
@Brain, just did the maths, i know in java the minimum class size is 8 bytes. At 8 bytes 2147483648 is almost 17 gig. So i guess we don't even get to the index being an integer as a problem. Yet...
uriDium
+1  A: 

I would have thought that the limit might be on the index. I thought that index used has to be an integer so anything bigger than integer wouldn't work unless they have some way around that. So that would be 4294967296 elements. Not sure if this is even half true. I would like to know the answer myself.

EDIT: As tomtom pointed out, integer is usually signed unless they using a non signed integer. So half of 4294967296 or 2147483648 roughly.

uriDium
it is pretty much double true. As arrays are integer indexed.... and start at 0 - only the positive part of the index is usable, roughly half your number ;) So, your answer is double true ;)
TomTom
If the index is an unsigned integer it would be your number.
Aurril
@TomTom, of course, forgot about the fact that it is signed.
uriDium
@TomTom: No it isn't. Each array is one object and since objects are restricted to 2 GB it depends on the type of elements the array holds.
Brian Rasmussen
@uriDium: The 2 GB limit will restrict this number significantly. You cannot have 2147483648 elements in an array.
Brian Rasmussen
@Brian Rasmussen: 2147483648 is the theoretical maximum number of elements, if each element would be 1 Byte long.
Aurril
@Aurril: You're right I should have stated that in most cases the 2 GB will limit the number of elements. Byte[] can get close to the theoretical maximum, but as arrays store a few additional pieces of data it is a little less than that.
Brian Rasmussen
The CLI specification allows Array indices to be either `int` or `long`. Microsoft chose to limit them to `int` in .NET, but Mono uses `long`, because Mono is used extensively in scientific high-performance cluster computing, and they frequently deal with arrays of much more than 2 billion elements.
Jörg W Mittag
+2  A: 

You will run into a practical limit first - it is pretty impossible to ge a 2gb array allocated. Practical limtis I have encountered are around the 800mb mark AT PROGRAM START - going down drastically after that.

Anything larger than 64mb is a luck gamble on 32 bit - the large object heap is not defragmented, so you need 65mn free in one piece or allocation fails.

Theoretical limits are:

  • usable memory, especially under 32 bit.
  • 32 bit number space for index (0 upward - no negative numbers for arrays UNLESS YOU PLAY SMART IN CREATION). You can create arrays allowing negative numbers, ut not with C# standard syntax - only with reflection.
  • 2gb per object.

But seriously, the practical implications are larger.

For .NET 4.0.... consider using memory mapped files ;)

TomTom
This thread is of course only relevant to 64-bit programs.
Hans Passant
The positive range (2GB) of int is enough for even a byte array...
Henk Holterman
A: 

Hope this help: http://blogs.msdn.com/joshwil/archive/2005/08/10/450202.aspx

i.e.  
 1. It uses int as index, which has max value = 2,147,483,647 (2GB)
 2. Its by design.
cornerback84