intArray 到 doubleArray,内存不足异常 C#
我正在尝试使用以下方法将 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,您正在尝试创建一个包含 1 亿个双精度数的数组(每个双精度数需要 800MB) - 两次:
为什么要费心将
myDoubleArray
初始化为空数组,然后重新分配价值?只需使用:这将使一件事所使用的内存量减半。现在我不确定它是否会在那时起作用......这取决于 Mono 如何处理大对象和内存。如果您使用大量内存,您一定要确保使用 64 位虚拟机。例如:(
使用此选项编译的重要程序集是将启动 VM 的主应用程序。目前尚不清楚您正在编写哪种应用程序。)
顺便说一句,
intarraytodoublearray
是一个可怕的名称 - 它使用别名int
而不是框架Int32
名称,并且忽略大小写约定。Int32ArrayToDoubleArray
会更好。Well, you're trying to create an array of 100 million doubles (each of which will take 800MB) - twice:
Why bother initializing
myDoubleArray
to an empty array and then reassigning the value? Just use: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:
(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 aliasint
instead of the frameworkInt32
name, and it ignores the capitalization conventions.Int32ArrayToDoubleArray
would be better.