I have a program in C#. I keep on adding elements to a dictionary. At a points when dictionary has 5999471 elements and when I add one more element it give out of memory exemption in mscorlib.dll.
Asked
Active
Viewed 2,118 times
1
-
5Seems like you already figured it out... – bobthedeveloper May 30 '14 at 06:53
-
1A question that rises is: Why do you need such a large collection in memory? I'm not being sceptical (yet), just curious (for now). – Silvermind May 30 '14 at 07:08
-
It looks like I need to store these elements in a database because it is not accepting more than 5999471 elements. – Sarveshwar May 30 '14 at 07:17
-
@Silvermind I am working on a permutation of a sequence of the numbers. For E.g. for 20 numbers I'll be producing 20!(Factorial) sets. So the result is so huge it is unable to fit in dictionary. – Sarveshwar May 30 '14 at 11:32
-
Then you have valid point regarding 'why so many' and Eric Lippert's intro in his answer for the duplicate makes more sense to you. – Silvermind May 30 '14 at 13:09
1 Answers
3
The capacity of a Dictionary is the number of elements the Dictionary can hold. As elements are added to a Dictionary, the capacity is automatically increased as required by reallocating the internal array.
For very large Dictionary objects, you can increase the maximum capacity to 2 billion elements on a 64-bit system by setting the enabled attribute of the gcAllowVeryLargeObjects configuration element to true in the run-time environment.
The gcAllowVeryLargeObjects enables arrays that are greater than 2 gigabytes (GB) in total size but only in 64bit machines.
Also keep in mind that that is the maximum amount of objects the Dictionary may hold, but your system memory may not allow you to go that far.

Nahuel Ianni
- 3,177
- 4
- 23
- 30
-
-
Performance? System stability? Ease of usage? Only they know for sure, but to me, even 2B items in memory is out of the question, you should be using a database and paging the results. – Nahuel Ianni May 30 '14 at 07:24
-
Could you please elaborate on how to use database or pagination here in this case. – Sarveshwar May 30 '14 at 07:57
-
1Too big of a topic to discuss here, but look for a tutorial on EntityFramework, that will get you started and solve your doubts. – Nahuel Ianni May 30 '14 at 08:14