intArray to doubleArray,Out of Memory Exception C#

Emm*_*t B 5 c# memory macos exception osx-snow-leopard

我试图使用以下方法将10,000 x 10,000的int数组转换为double数组(我在本网站中找到)

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;
}
Run Code Online (Sandbox Code Playgroud)

我打电话的方式是

 int bound0 = myIntArray.GetUpperBound(0);
 int bound1 = myIntArray.GetUpperBound(1);
 double[,] myDoubleArray = new double[bound0,bound1];  
 myDoubleArray = intarraytodoublearray(myIntArray)  ;
Run Code Online (Sandbox Code Playgroud)

它给了我这个错误,

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)
Run Code Online (Sandbox Code Playgroud)

该机器有32GB RAM,OS是MAC OS 10.6.8

Jon*_*eet 5

好吧,你正在尝试创建一个包含1亿个双打的阵列(每个都需要800MB) - 两次:

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

为什么要打扰初始化myDoubleArray为空数组然后重新分配值呢?只需使用:

double[,] myDoubleArray = intarraytodoublearray(myIntArray);
Run Code Online (Sandbox Code Playgroud)

这将使用于一件事的内存量减半.现在,无论它是否会起作用,我都不确定......这取决于Mono如何处理大型物体和记忆.如果你使用大量内存,你肯定想确保使用的是64位虚拟机.例如:

gmcs -platform:x64 ...
Run Code Online (Sandbox Code Playgroud)

(使用此选项进行编译的重要程序集是启动VM的主要应用程序.目前尚不清楚您正在编写什么类型的应用程序.)

另外,它intarraytodoublearray是一个可怕的名称 - 它使用别名int而不是框架Int32名称,它忽略了大写的约定.Int32ArrayToDoubleArray会更好.