I'm experimenting with memory limits on my x64 machine (Windows7) under .NET 4.5. Based on this documentation (as referred to in this answer) it seems like I should be able to create an array with up to UInt32.MaxValue
elements. So I enabled gcAllowVeryLargeObjects
and tried to create the following array:
int[] arr = new int[UInt32.MaxValue]; //2^32
However, this generates an OverflowException: ("Arithmetic operation resulted in an overflow.")
So I tried this instead,
int[] arr = new int[UInt32.MaxValue/2]; //2^31 (i.e., 2 * 1024 * 1024 * 1024)
and it throws a 'System.OutOfMemoryException'.
What am I missing here, and what in fact is the largest integer array that I can create?
UPDATE: So, as it turns out, the largest array I can create is:
int[] arr = new int[(long)(1.999 * 1024 * 1024 * 1024)]; //just shy of 2^31
So why can't I reach the limit that the documentation leads me to expect?