循环导致内存不足异常 [英] loop results in out of memory exception

查看:109
本文介绍了循环导致内存不足异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这个循环在5次迭代后导致内存不足异常?

如何解决内存不足异常?我需要对具有2列和15000000行的矩阵进行计算?如果我删除test.Add行然后它可以工作吗?

这是一个多维向量,我需要做其他操作



< pre lang =xml>列表< double [,] > test = new List < double [,] > ();
for(int i = 0; i < 100000; i ++)

{

double [,] array = new double [15000000, 2];

test.Add(array);

< span class =code-attribute>
}

解决方案

购买更多内存。



说真的,你为什么需要这些100,0000的集合?


假设您可以在此代码中创建您正在尝试的内容,您会意识到您将创建一个包含100,000个节点的列表,其中每个节点都是一个数组有15,000,000行2列? (如上所述,228 Meg ?运行它意味着你需要一台内存超过20TB的机器!(如果我的数学运算正确的话)我不确定你在尝试什么要完成,但我看不到很多情况,这是解决问题的合理方法。



内存问题可能不是系统中的内存本身,因为它是.net运行时处理内存管理的方式。由于你声明和初始化对象的方式,它们将在堆上创建,通常最大为1GB。如上所述。你会很快超过它。



有一些处理大型物体的策略可以帮助应用程序性能,但我们不需要覆盖它......你的代码甚至不在这些策略有用的星球上。



我建议回到绘图板并从另一个角度处理你的问题。如何将其分解成适合普通计算机的较小块。可能需要100或1000甚至1,000,000次传递来处理所有内容,但至少它能够完成任务。






是的。您的RAM在该循环中遇到困难,并且在5个周期后内存不足。双数据类型需要64位(8字节)。所以15000000 * 2 * 8 = 240000000字节(228.88 MB)。因此在第5个周期中它传递1GB标记为228.88 * 5 = 1144.41 MB。我猜你的RAM是1GB。



问候。


Why does this loop result in an out of memory exception after 5 iterations?
How can I solve the out of memory exception? I need to do calculations on a matrix that has 2 columns and 15000000 rows?? If I remove the test.Add line then it works?
It is a multidimensional vector which I need to do other operations on

List<double[,]> test = new List<double[,]>();
            for (int i = 0; i < 100000; i++)

            {

                double[,] array = new double[15000000, 2];

                test.Add(array);

            }

解决方案

Buy more RAM.

Seriously, why do you need a collection of 100,0000 of these ?


Assuming that you could create what you are trying in this code, you realize that you would create a list of 100,000 nodes where each node was an array with 15,000,000 rows of 2 columns? (228 Meg EACH as noted above? Running that out means you need a machine with over 20TB of RAM! (if I''m doing the math correct) I''m not sure what you are trying to accomplish but I can''t see a lot of situations where this would be a reasonable approach to solving a problem.

The memory issue probably isn''t the memory in your system, per se, as it is the way the .net runtime handles memory management. Because of the way you are declaring and initializing your objects, they would be created on the heap which normally maxes out at 1GB. As noted, you would rapidly surpass that.

There are strategies for dealing with large objects that can help with application performance but we don''t need to cover that... Your code isn''t even on a planet where those strategies would be useful.

I suggest going back to the drawing board and approaching your problem from a different angle. Look for ways to break it into smaller chunks that will fit into a normal computer. Maybe it takes 100 or 1000 or even 1,000,000 passes to process everything but at least it would be able to accomplish the task.


Hi,

Yes. Your RAM had a hard time with that loop, and ran out of memory after 5 cycles. Double data type takes 64 bits (8 bytes). So 15000000 * 2 * 8 = 240000000 bytes (228.88 MB). So in 5th cycle it pass the 1GB mark as 228.88 * 5 = 1144.41 MB. I guess your RAM is 1GB.

Regards.


这篇关于循环导致内存不足异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆