Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

XML
List<double[,]> test = new List<double[,]>();
            for (int i = 0; i < 100000; i++)
            {
                double[,] array = new double[15000000, 2];
                test.Add(array);
            }
Posted
Updated 12-Dec-12 20:13pm
v2
Comments
Jibesh 13-Dec-12 2:12am    
too many copies of same question!!! do update your question with more details rather duplicating the questions.
codeninja-C# 13-Dec-12 2:19am    
May i know your RAM Size? probably, it takes around 1GB RAM for 5 iterations
--Sj
CPallini 13-Dec-12 16:40pm    
If you need to do calculation on a matrix then why are attempting to create 100000 of them?

Buy more RAM.

Seriously, why do you need a collection of 100,0000 of these ?
 
Share this answer
 
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.
 
Share this answer
 
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900