3

My program generates OutOfMemoryExcetion only in Release mode, compiling with VS2010 C# compiler. The operation that it was performing when the crash occurred was instantiating 1600 jagged arrays, and it broke when at the 207th of the simplified version of the loop below:

Double[][] arr = new Double[1600][];
int i = 0;
while (i < 1600)
{
    arr[i] = new Double[1000000];
    i++;
}

Basically, just this part of code would generate ~ 11.9 GB, considering that a Double consumes 8 bytes. I don't have that amount of RAM, but in another question where I asked what happens when the RAM is over, they answered me that the CLR creates a swap memory, though it can only address a well defined amount of memory.

When I compile my program in DEBUG mode, it doesn't throw the OutOfMemoryException, instead, it allocates a lot of memory and keep the program running... I didn't have enough patience to check if the program would run smooth, though slowly, until the end, because it was taking too long.

So:

  • what did happen in Release mode that didn't in Debug mode, causing the program to throw an Exception there?
  • considering I really need to store that amount of memory, because it is the data generated by some scientific computations, what are your advices to handle this problem? (I'm considering processing the generated data during the calculations, though it would take a little more time during the calculations itself, instead of generating a lot of data and then processing it...)
Community
  • 1
  • 1
Girardi
  • 2,734
  • 3
  • 35
  • 50
  • Note that the maximum amount of managed memory you can address in a 32-bit process is 2GB (in case you haven't compiled to an x64 target). – Dirk Vollmar May 26 '11 at 18:24
  • 2
    Also, release mode is usually faster, so it might just be that you didn't wait long enough for the `OutOfMemory` exception to occur in the debug build. – Dirk Vollmar May 26 '11 at 18:25

1 Answers1

9

Assuming you created your project in Visual Studio 2010, its platform target would have defaulted to x86, i.e., a 32-bit program. I'm assuming you changed this to x64 already, or you never would have been able to allocate more than 2GB of address space.

My guess is that you just went to project properties > Build tab, and changed "Platform target". The thing is, if that's all you did, that would only affect your Debug build configuration, because the "Configuration" filter at the top of the screen defaults to "Active (Debug)".

Change that filter box to "Release" to see your release build settings. You'll need to change "Platform target" to "x64" here as well.

Joe White
  • 94,807
  • 60
  • 220
  • 330
  • That was it... Sorry for my ignorance, but isn't strange that a x86 program, like my VS2010, can compile to a x64 processor? (I'm a physicist, not a computational scientist!) – Girardi May 29 '11 at 23:13
  • Not strange at all. All it needs to do is write the correct bytes for x64 machine code. It's not unusual to compile apps for a new platform (e.g. iPhone, Android, Windows Phone 7) using a compiler that runs on a completely different OS (e.g. Mac, Windows). – Joe White May 31 '11 at 01:25