Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

intArray to doubleArray, Out Of Memory Exception C#

I am trying to convert a 10,000 by 10,000 int array to double array with the following method (I found in this website)

public double[,] intarraytodoublearray( int[,] val){ 

        int rows= val.GetLength(0);
        int cols = val.GetLength(1);
        var ret = new double[rows,cols];
        for (int i = 0; i < rows; i++ )
        {

            for (int j = 0; j < cols; j++) 
            {
                ret[i,j] = (double)val[i,j];
            }
        }
        return ret;
}

and the way I call is

 int bound0 = myIntArray.GetUpperBound(0);
 int bound1 = myIntArray.GetUpperBound(1);
 double[,] myDoubleArray = new double[bound0,bound1];  
 myDoubleArray = intarraytodoublearray(myIntArray)  ;

it gives me this error,

Unhandled Exception: OutOfMemoryException
[ERROR] FATAL UNHANDLED EXCEPTION: System.OutOfMemoryException: Out of memory
at (wrapper managed-to-native) object:__icall_wrapper_mono_array_new_2 (intptr,intptr,intptr)

The machine has 32GB RAM, OS is MAC OS 10.6.8

like image 788
Emmet B Avatar asked Jan 23 '12 08:01

Emmet B


1 Answers

Well, you're trying to create an array of 100 million doubles (each of which will take 800MB) - twice:

// This line will allocate an array...
double[,] myDoubleArray = new double[bound0,bound1];  
// The method allocates *another* array...
myDoubleArray = intarraytodoublearray(myIntArray);

Why bother initializing myDoubleArray to an empty array and then reassigning the value? Just use:

double[,] myDoubleArray = intarraytodoublearray(myIntArray);

That will halve the amount of memory used for one thing. Now whether or not it'll work at that point, I'm not sure... it depends on how Mono deals with large objects and memory. If you're using a lot of memory, you definitely want to make sure you're using a 64-bit VM. For example:

gmcs -platform:x64 ...

(The important assembly to compile with this option is the main application which will start up the VM. It's not clear what kind of application you're writing.)

As an aside, intarraytodoublearray is a horrible name - it uses the alias int instead of the framework Int32 name, and it ignores the capitalization conventions. Int32ArrayToDoubleArray would be better.

like image 59
Jon Skeet Avatar answered Nov 12 '22 02:11

Jon Skeet