5

im trying to create the following array

int numOfArrays = 50000;
int lengthOfArrays = 13500;

long[,] unsortedNumbers = new long[numOfArrays,lengthOfArrays];

but i keep getting the out an memory exception. Im targeting x64, i believe ive set the large-address-aware flag, see pic, yet im still getting the error. Odd thing is, i have a list in the same program which consumes 16gig of ram without any issues.

System:

64gig ram

100gig free on hd.

Hans Rudel
  • 3,433
  • 5
  • 39
  • 62
  • Which framework ? There are differences in memory handling for large objects in FW2.0 and FW4.0. – Ondrej Svejdar Jul 23 '13 at 11:32
  • .net 4 client profile and also tried .net 4 – Hans Rudel Jul 23 '13 at 11:36
  • First, see http://stackoverflow.com/questions/8563933/c-sharp-out-of-memory-exception. Take a look to http://stackoverflow.com/questions/750574/how-to-get-memory-available-or-used-in-c-sharp and http://stackoverflow.com/questions/3803003/c-sharp-memory-usage. Also try to use System.Int32.MaxValue, as you can see in http://stackoverflow.com/questions/1391672/what-is-the-maximum-size-that-an-array-can-hold. – Mihai8 Jul 23 '13 at 11:39
  • @Anirudh - long is 8 bytes, 8 * 50000 * 13500 gives around 5GB – Ondrej Svejdar Jul 23 '13 at 11:39

1 Answers1

12

There's a 2Gig Limit on the size of objects in the .NET runtime for both 32bit and 64bit processes.

But in NET 4.5 you can increase the limit of NET code which is running on the runtime in a 64bit process with gcAllowVeryLargeObjects.

Your NET code will be running as 64bit if:

  • your Platform Target says "x64"
  • you are using NET 4 and your Platform Target says "AnyCPU" and you are running on a 64bit OS platform
  • you are using NET 4.5 and your Platform Target says "AnyCPU" and you have "prefer 32-bit" off/unticked and you are running on a 64bit OS platform

You are allocating:

50000 * 13500 * 8 = 5400000000 bytes = 5.029 gigabytes

If you don't have the luxury of using NET 4.5 then depending on your usage scenario you may be able to use BigArray instead:

Colin Smith
  • 12,375
  • 4
  • 39
  • 47