I have been looking at some SO questions related to the max size of an array of bytes (here and here) and have been playing with some arrays and getting some results I don't quite understand. My code is as follows:
byte[] myByteArr;
byte[] myByteArr2 = new byte[671084476];
for (int i = 1; i < 2; i++)
{
myByteArr = new byte[671084476];
}
This will compile and upon execution it will throw a 'System.OutOfMemoryException'
on the initialization of myByteArr
. If I change the 2 in the for loop to a 1 or I comment out one of the initialization's (either myByteArr2
or myByteArr
) it will run fine.
Also, byte[] myByteArr = new byte[Int32.MaxValue - 56];
throws the same exception.
Why does this happen when compiled for 32-bit? Aren't they within the 2GB limit?