Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6909923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:45:01+00:00 2026-05-27T08:45:01+00:00

There’s probably 5 or 6 SO posts that tangentially touch on this, but none

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:45:01+00:00Added an answer on May 27, 2026 at 8:45 am

    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 IDictionary functionality, you could implement the interface, delegating down to the “real” dictionary and modifying the cumulative size value in the Add and Remove operations. If you don’t need your cache to expose the full IDictionary functionality, simply define a stripped down interface (with maybe just Add, Contains, and Remove methods and a CumulativeSize property. Or, you might decide to implement a caching object without an interface. If it were me, I would either use IDictionary or define an interface, like ICache.

    So, your cache might look something like this (uncompiled and untested):

    public interface ICacheWithCumulativeSize
    {
      void Add(string key, string value);
      bool Contains(string key);
      void Remove(string key);
      int CumulativeSize { get; }
    }
    
    public class MyCache : ICacheWithCumulativeSize
    {
      private IDictionary<string, string> dict = new Dictionary<string, string>();
    
      public void Add(string key, string value)
      {
        CumulativeSize += value.Length;
        dict[key] = value;
      }
    
      public bool Contains(string key)
      {
        return dict.ContainsKey(key);
      }
    
      public void Remove(string key)
      {
        string toRemove = dict[key];
        CumulativeSize -= value.Length;
        dict.Remove(key);
      }
    
      int CumulativeSize { public get; private set; }
    }
    

    This is pretty rough. Obviously it could be more efficient and more robust. I am not doing any checking in Add and Remove to 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 the CumulativeSize when 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.

      public class MySpecialDictionary : IDictionary<string, string>
      {
        private IDictionary<string, string> dict = new Dictionary<string, string>();
    
        public int TotalSize { get; private set; }
    
        #region IDictionary<string,string> Members
    
        public void Add(string key, string value)
        {
          dict.Add(key, value);
          TotalSize += string.IsNullOrEmpty(value) ? 0 : value.Length;
        }
    
        public bool ContainsKey(string key)
        {
          return dict.ContainsKey(key);
        }
    
        public ICollection<string> Keys
        {
          get { return dict.Keys; }
        }
    
        public bool Remove(string key)
        {
          string value;
          if (dict.TryGetValue(key, out value))
          {
            TotalSize -= string.IsNullOrEmpty(value) ? 0 : value.Length;
          }
          return dict.Remove(key);
        }
    
        public bool TryGetValue(string key, out string value)
        {
          return dict.TryGetValue(key, out value);
        }
    
        public ICollection<string> Values
        {
          get { return dict.Values; }
        }
    
        public string this[string key]
        {
          get
          {
            return dict[key];
          }
          set
          {
            string v;
            if (dict.TryGetValue(key, out v))
            {
              TotalSize -= string.IsNullOrEmpty(v) ? 0 : v.Length;
            }
            dict[key] = value;
            TotalSize += string.IsNullOrEmpty(value) ? 0 : value.Length;
          }
        }
    
        #endregion
    
        #region ICollection<KeyValuePair<string,string>> Members
    
        public void Add(KeyValuePair<string, string> item)
        {
          dict.Add(item);
          TotalSize += string.IsNullOrEmpty(item.Value) ? 0 : item.Value.Length;
        }
    
        public void Clear()
        {
          dict.Clear();
          TotalSize = 0;
        }
    
        public bool Contains(KeyValuePair<string, string> item)
        {
          return dict.Contains(item);
        }
    
        public void CopyTo(KeyValuePair<string, string>[] array, int arrayIndex)
        {
          dict.CopyTo(array, arrayIndex);
        }
    
        public int Count
        {
          get { return dict.Count; }
        }
    
        public bool IsReadOnly
        {
          get { return dict.IsReadOnly; }
        }
    
        public bool Remove(KeyValuePair<string, string> item)
        {
          string v;
          if (dict.TryGetValue(item.Key, out v))
          {
            TotalSize -= string.IsNullOrEmpty(v) ? 0 : v.Length;
          }
          return dict.Remove(item);
        }
    
        #endregion
    
        #region IEnumerable<KeyValuePair<string,string>> Members
    
        public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
        {
          return dict.GetEnumerator();
        }
    
        #endregion
    
        #region IEnumerable Members
    
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
          return dict.GetEnumerator();
        }
    
        #endregion
      }
    

    Good luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is this strange situation I'm fighting on. I have 3 pages, les call
There are many tutorials that talk about deleting index.php from the url. But I
There must be a simple solution to this, but after 4 hours of browsing
There are things like f.call(...) f.apply(...) But then there's this (1, alert)('Zomg what is
There is about 2000 lines of this, so manually would probably take more work
There are some posts that asks what the difference between those two are already.
There is a conversion process that is needed when migrating Visual Studio 2005 web
There is previous little on the google on this subject other than people asking
There are numerous Agile software development methods. Which ones have you used in practice
There is a field in my company's Contacts table. In that table, there is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.