views:

562

answers:

1

I've read (http://blogs.msdn.com/joshwil/archive/2005/08/10/450202.aspx) that the maximum size of an object in .NET is 2 GB.

Am I correct in assuming that if I have an Object that takes up 256 MB Memory, since it is a reference type, I can have an array of these 256 MB Objects where all the objects together may takeup >2GB Memory as long as the size of the reference array stays below 2 GB?

+4  A: 

Yes, your assumption is correct.

The 2GB limit applies to each object individually. The total memory used for all objects can exceed 2GB.

(Whether the runtime is able to allocate enough memory for your requirements is another matter. I doubt if it could find a full 2GB of spare memory on a 32bit machine, but it shouldn't be a problem on 64bit.)

LukeH
So the "reference array" will be an object that would (typically) take up the same amount of space as an "integer array"?
Nate Bross
Nate > it is an implementation detail but references are currently pointers and so they have the same size as the pointer size (In C it was the same as the sizeof(int) on most systems but in C# int always mean Int32)
VirtualBlackFox
This is bizarre considering that .NET's Array class has special methods that take "long" array length arguments. If the limit is 2GB then it is impossible for an array length to overflow a 32-bit int.
Qwertie
@Qwertie: If i did the math right, 2GB of Int32 objects in an array should take 549,755,813,888 indices, which is indeed out of the range for an Int32. Make that an array of bytes, and it's 4x bigger.
Will Eddins
@Qwertie: The Array members that take or return Int64 just cast those values to Int32 internally and, in the case of parameters, will throw an ArgumentOutOfRangeException if a long parameter isn't between int.MinValue and int.MaxValue.
LukeH