13,147,183 members (28,311 online)
Rate this:
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

List<double[,]> test = new List<double[,]>();
for (int i = 0; i < 100000; i++)
{
double[,] array = new double[15000000, 2];
}
Posted 12-Dec-12 20:07pm
Updated 12-Dec-12 20:13pm
v2
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?

Rate this:

## Solution 1

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

## Solution 3

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.
Rate this:

## Solution 2

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.

Top Experts
Last 24hrsThis month
 OriginalGriff 265 Graeme_Grant 50 CPallini 50 RickZeeland 40 Jochen Arndt 30
 OriginalGriff 6,125 Graeme_Grant 5,101 ppolymorphe 1,994 Jochen Arndt 1,904 CPallini 1,855