intArray到doubleArray,内存不足异常C#

本文关键字:异常 内存不足 doubleArray intArray | 更新日期: 2023-09-27 18:21:12

我正试图用以下方法将10000乘10000的int数组转换为双数组(我在这个网站上找到)

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;
}

我的电话是

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

它给了我这个错误,

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)

机器具有32GB RAM,操作系统为MAC操作系统10.6.8

intArray到doubleArray,内存不足异常C#

好吧,您正试图创建一个包含1亿个双打(每个双打需要800MB)的数组-两次

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

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

double[,] myDoubleArray = intarraytodoublearray(myIntArray);

这将使用于一件事的内存量减半。现在,我不确定它在那一点上是否有效。。。这取决于Mono如何处理大型对象和内存。如果你使用了大量内存,你肯定要确保你使用的是64位虚拟机。例如:

gmcs -platform:x64 ...

(使用此选项编译的重要程序集是将启动VM的主要应用程序。目前尚不清楚您正在编写的是哪种应用程序。)

顺便说一句,intarraytodoublearray是一个可怕的名称——它使用别名int而不是框架Int32的名称,并且它忽略了大写约定。CCD_ 5会更好。