Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OutOfMemoryException on declaration of Large Array

Tags:

c#

I have to create a fairly large double array 12000ish x 55000ish. Unfortunately, I get an out of memory exception. I used to develop in Java and could change the memory settings. Is this possible with C# or is it just impossible? I am using VS 2008.

like image 943
cs0815 Avatar asked Jan 27 '11 11:01

cs0815


People also ask

What causes OutOfMemoryException?

When data structures or data sets that reside in memory become so large that the common language runtime is unable to allocate enough contiguous memory for them, an OutOfMemoryException exception results.

How can we avoid Outofmemory exception?

1) An easy way to solve OutOfMemoryError in java is to increase the maximum heap size by using JVM options "-Xmx512M", this will immediately solve your OutOfMemoryError.


1 Answers

Each double is 8 bytes, so you're trying to allocate a single array with just over 5GB. The CLR has a per-object limit of around 2GB IIRC, even for a 64-bit CLR. In other words, it's not the total amount of memory available that's the problem (although obviously you'll have issues if you don't have enough memory), but the per-object size.

I suggest you split it into smaller arrays, perhaps behind a facade of some description. I don't believe there's any way to workaround that limit for a single array.

EDIT: You could go for an array of arrays - aka a jagged array:

double[][] array = new double[12000][];
for (int i = 0; i < array.Length; i++)
{
    array[i] = new double[55000];
}

Would that be acceptable to you?

(You can't use a rectangular array (double[,]) as that would have the same per-object size problem.)

like image 151
Jon Skeet Avatar answered Oct 25 '22 05:10

Jon Skeet