Register memory usage in memory

There are probably 5 or 6 SO posts that relate tangentially, but no one answers the question.

I have a Dictionary object that I use as a cache for storing values. The problem is that I don’t know how big it is - with time it can become big or not, but I can’t say, so I can’t evaluate its effectiveness or draw conclusions about how the user uses the software, because this is part , which will go into production and control something for a very long period of time, it makes no sense to attach a memory profiler or something like that to debug.

Ideally, I would just put a call into 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");
   ...
}

Is it possible to do this without binding some debugging tool so that you can use the deployed script?

+5
source share
2 answers

Given your last comment that the value is a variable-length string, it should be simple enough to calculate the size of each element in the dictionary. I would like to save time and effort by creating my own cache object (perhaps just by wrapping the dictionary) and keeping track of the total size as items are added and removed from the cache. Thus, at any point in time, you can specify the total size of the values ​​in the cache by viewing the value that you tracked all the time.

IDictionary, , "" Add Remove. , IDictionary, (, Add, Contains Remove CumulativeSize). . , IDictionary, , ICache.

, (uncompiled 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; }
}

. , . Add Remove, , .., , , , . , , , , (, , ), , CumulativeSize, , , , . , . , , .

... , , IDictionary . , . , , - ... , . , . , , .

  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
  }

!

+1

, , .

, , , , .

+1

All Articles