There’s probably 5 or 6 SO posts that tangentially touch on this, but none really answer the question.
I have a Dictionary object I use kind of as a cache to store values. The problem is I don’t know how big it is getting — over time it might be growing big or not, but I cannot tell and so I can’t gauge its effectiveness, or make conclusions about how a user is using the software. Because this is a piece that will go into production and monitor something over a very long period of time, it doesn’t make sense to attach memory profiler or anything debuggy like that.
Ideally, I would simply place a call in my Timer that would do something like:
private void someTimer_Tick(object sender, EventArgs e)
{
...
float mbMem = cacheMap.GetMemorySize();
RecordInLog(DateTime.Now.ToString() + ": mbMem is using " + mbMem.ToString() + "MB of memory");
...
}
Can this be done without attaching some debug tool so that it can be used deployed scenario?
Given your most recent comment, that the value is a variable length string, it should be easy enough to calculate the size of each item in the dictionary. I would consider saving time and effort by creating your own caching object (possibly just wrapping a Dictionary) and keeping track of the total size as items are added to and removed from the cache. This way, at any point in time you can tell the total size of the values in the cache by looking the value that you have been keeping track of all along.
If you need your cache to expose the full
IDictionaryfunctionality, you could implement the interface, delegating down to the “real” dictionary and modifying the cumulative size value in theAddandRemoveoperations. If you don’t need your cache to expose the fullIDictionaryfunctionality, simply define a stripped down interface (with maybe justAdd,Contains, andRemovemethods and aCumulativeSizeproperty. Or, you might decide to implement a caching object without an interface. If it were me, I would either useIDictionaryor define an interface, likeICache.So, your cache might look something like this (uncompiled and untested):
This is pretty rough. Obviously it could be more efficient and more robust. I am not doing any checking in
AddandRemoveto see if a key already exists, etc, but I think you probably get the idea. Also, it is possible that the strings that are stored as values in the dictionary could be modified externally (maybe not in your program, but theoretically), so the length of a string when it is subtracted from theCumulativeSizewhen a value is removed from the cache might not be the same as the length of that string when it was originally added. If this is is a concern, you could consider storing copies of the values in the internal dictionary. I don’t know enough about your application to say whether this is a good idea or not.For completeness… Here is a rough implementation that simply wraps a dictionary, exposes the IDictionary interface, and keeps track of the total size of items in the cache. It has a little more defensive code, primarily to protect the size accumulator. The only part that I might consider tricky is the index setter… My implementation checks to see if the index being set already exists. If so, the cumulative value is decremented appropriately and then incremented based on the size of the input value. Otherwise, I think it is pretty straightforward.
Good luck!