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

发布于 2024-12-28 08:02:51 字数 1000 浏览 3 评论 0原文

我正在尝试使用以下方法将 10,000 x 10,000 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 OS 10.6.8

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爺獨霸怡葒院 2025-01-04 08:02:51

好吧,您正在尝试创建一个包含 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 名称,并且忽略大小写约定。 Int32ArrayToDoubleArray 会更好。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文