Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Out of memory exception on 64bit

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.

like image 449
Hans Rudel Avatar asked Jul 23 '13 11:07

Hans Rudel


People also ask

How do I resolve system out of memory exception?

If you are using large arrays or other collection objects whose size results in an OutOfMemoryException exception, you should modify your application to work the data in subsets rather than to work with it all at once.

What causes a memory exception?

Causes of such memory errors may be due to certain cognitive factors, such as spreading activation, or to physiological factors, including brain damage, age or emotional factors. Furthermore, memory errors have been reported in individuals with schizophrenia and depression.

Can you catch an out of memory exception?

Sure, catching OutOfMemoryError is allowed. Make sure you have a plan for what to do when it happens. You will need to free up some memory (by dropping references to objects) before allocating any more objects, or you will just run out of memory again.


1 Answers

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.

  • https://docs.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/runtime/gcallowverylargeobjects-element

  • Memory limitations in a 64-bit .Net application?

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:

  • http://blogs.msdn.com/b/joshwil/archive/2005/08/10/450202.aspx
like image 152
CSmith Avatar answered Sep 21 '22 17:09

CSmith