vasanth
vasanth

Reputation: 11

How to improve performance while adding huge data into Dictionary?

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.

Upvotes: 1

Views: 174

Answers (1)

spender
spender

Reputation: 120548

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.

Upvotes: 2

Related Questions