Sunday, May 20, 2018

c# - OutOfMemoryException only in Release mode when compiled with VS2010?




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...)


Answer



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.


No comments:

Post a Comment

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...