I am using a Dictionary to store data, and will be caching it. I would like to avoid server memory issues, and have good performance by limiting the size of the Dictionary<>, either in size or number of entries.
What is the best method of doing this? Is there another class I should be considering other than a Dictionary?

- 1,923
- 4
- 24
- 40

- 4,826
- 1
- 23
- 26
5 Answers
You don't need to limit the size of the Dictionary to archieve good performance.
As the documentation says:
Retrieving a value by using its key is very fast, close to O(1)

- 5,675
- 21
- 29
-
2If you allow the dictionary to grow unbounded, you can wind up using all your addressable memory. Dependening on the hardware, you may or may not hit performance problems because of swapping long before that. – Malcolm Post Apr 30 '10 at 13:45
There are several other classes you can select from like
you can review the options by looking at System.Collections.Generic Namespace.
Their is a very good post, describibg pros and cons for all most of the collection classes at MSDN
If you are not satisfied by what these classes are provide, you can go for your own collection class or design a custom Dictionary
your self.
you will need to inherit your custom dictionary fron IDictionary Interface and other classes / interfaces or may write all from scratch.
Here is the signature for Dictionary class at MSDN
[SerializableAttribute]
[ComVisibleAttribute(false)]
public class Dictionary<TKey, TValue> : IDictionary<TKey, TValue>,
ICollection<KeyValuePair<TKey, TValue>>,
IEnumerable<KeyValuePair<TKey, TValue>>,
IDictionary, ICollection, IEnumerable,
ISerializable, IDeserializationCallback

- 21,468
- 17
- 69
- 94
-
i have no need for sorting. Dictionary works well for me as I can get the value by key. My main concern is the size the Dictionary may grow to. – derek Apr 30 '10 at 13:38
-
I have just given you some more options to look at. Why not derive your own dictionary and `overright` its growth rules with the ones you prefer ? – Asad Apr 30 '10 at 13:43
-
-
Just curious... What should happen when the limit is reached? Silently drop, throw exception, remove the first cached item, remove the item that hasn't been accessed for the longest time, etc.? For removing the first cached item, use a custom Queue. For the item that hasn't been accessed, you need to keep track of that time, etc. You may also want to consider using the built-in caching mechanisms. – Nelson Rothermel Apr 30 '10 at 16:51
-
The link "Selecting a Collection Class" in your answer above has the following quote near the end: "The SortedDictionary generic class provides faster lookup than the Dictionary generic class.". I don't think this is true. Dictionary is O(n). SortedDictionary is O(log n). See [here](http://social.msdn.microsoft.com/Forums/vstudio/en-US/7513bbe6-5bd3-4309-8cca-7056043fb95a/sorteddictionary-vs-dictionary?forum=csharpgeneral). – Wes Apr 08 '14 at 17:31
You will need to create your own class inheriting from the Dictionary class. Override the Add and set Item methods to limit the number of entries as objects are added.

- 515
- 5
- 9
"A cache without an expiration policy is just a memory leak."
(Sorry, unattributed as I don't know who first said it.)

- 2,757
- 1
- 17
- 24
-
the cache does have an non-sliding expiration policy, and a dependency, just want to make sure it doesn't grow too big within that expiration window. – derek Apr 30 '10 at 15:34
One thing to keep in mind is that when you initialize a Dictionary you can set the initial capacity. If you know how big your list will be, set it to the correct size and you won't have any "wasted" space. If you don't specify the capacity, it will set a default starting capacity and resize/grow as needed, which takes a performance hit.

- 9,436
- 8
- 62
- 81
-
the size will be variable, so the growth of it is my main concern. I would like to limit the max capacity since it will be stored in the server's cache. This will be part of a web application. – derek Apr 30 '10 at 13:35
-