1

How to improve memory usage and performance while adding 1000000 values into Dictionary in C#.

While adding huge records I am getting "System out of Memory" Exception.

Can anyone help on this.

My Dictionary will be like below.

    Dictionary<string, List<string>>

Thanks in advance.

vasanth
  • 11
  • 1
  • 3
    What does your code look like at the moment? – ChrisBint Jun 22 '12 at 09:49
  • How physically big is the data you are trying to place into your dictionary? After all each of your lists could contain many items, as well as the many entries in your dictionary... – BugFinder Jun 22 '12 at 09:52
  • http://stackoverflow.com/questions/3657181/very-large-collection-in-net-causes-out-of-memory-exception – S2S2 Jun 22 '12 at 09:55

1 Answers1

2

What are you encoding in your Dictionary? How are you adding to it? Perhaps the Linq extension ToLookup might be more suitable here. ILookup provides more or less the same functionality as creating an IDictionary<TKey,IList<TValues>>

Otherwise... Be explicit when setting the size of the Dictionary:

new Dictionary<string,List<string>>(int capacity)

and also the List instances you store in it

new List<string>(int capacity)

This stops the growing algorithm (which doubles capacity when it is reached) from being deployed and thrashing memory.

spender
  • 117,338
  • 33
  • 229
  • 351