Click here to Skip to main content
11,926,472 members (54,916 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#
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 21:07pm
Edited 12-Dec-12 21:13pm
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
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: bad
Please Sign up or sign in to vote.

Solution 1

Buy more RAM.

Seriously, why do you need a collection of 100,0000 of these ?
Rate this: bad
Please Sign up or sign in to vote.

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: bad
Please Sign up or sign in to vote.

Solution 2


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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.151126.1 | Last Updated 13 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100