title: Memory usage of Dictionaries in C#
body: <p>I have some code that I added a nested dictionary to, of the following format</p>
  
  <pre><code>Dictionary&lt;string, Dictionary&lt;string, Dictionary&lt;string, float&gt;&gt;&gt;
  </code></pre>
  
  <p>After doing so I noticed the memory usage of my application shot up SIGNIFICANTLY.  These dictionaries are keyed on strings that are often repeated, and there are many of these dictionaries, on the order of 10's of thousands.</p>
  
  <p>In order to address this problem I hypothesized that the repeated strings were eating up a significant amount of memory.  My solution was to hash the strings and use an integer instead (I would keep one copy of a rainbow table so I could reverse the hash when necessary)</p>
  
  <pre><code>Dictionary&lt;int, Dictionary&lt;int, Dictionary&lt;int, float&gt;&gt;&gt;
  </code></pre>
  
  <p>So I went to a memory profiler to see what kind of size reduction I could get.  To my shock I actually found that the string storage was actually smaller in size (both normal and inclusive).</p>
  
  <p>This doesn't make intuitive sense to me.  Even if the compiler was smart enough to only store one copy of the string and use a reference, I would think that reference would be a pointer which is double the size of an int.  I also didn't use any <code>String.Intern</code> methods so I don't know how this would have been accomplished (also is <code>String.Intern</code> the right method here?)</p>
  
  <p>I'm very confused as to what's happening under the hood, any help would be appreciated</p>