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];
Edited 12-Dec-12 20:13pm
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.
Buy more RAM.
Seriously, why do you need a collection of 100,0000 of these ?
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.
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)