anouar.bagari anouar.bagari - 1 month ago 8
C# Question

Why the Allocation of an array with int.MaxValue fails and the allocation of two arrays with the size int.MaxValue / 2 does not?

Why the allocation of a byte array with the size

int.MaxValue
fails

byte[] array1 = new byte[int.MaxValue]; // throws an OutOfMemoryException


and the allocation of two arrays with the size
int.MaxValue / 2
does not?

byte[] array2 = new byte[int.MaxValue / 2];
byte[] array3 = new byte[int.MaxValue / 2];

Answer

The maximum size of an object in .NET is 2GB: http://blogs.msdn.com/b/joshwil/archive/2005/08/10/450202.aspx

int.MaxValue + the array overhead is slightly greater than 2 GB.

Comments