title: Deduplicate string instances
body: <p>I have array of nearly 1,000,000 records, each record has a field "filename".</p>
  
  <p>There are many records with exactly the same filename.</p>
  
  <p>My goal is to improve memory footprint by deduplicating string instances (filename instances, not records).</p>
  
  <p>.NET Framework 2.0 is a constraint. no LINQ here.</p>
  
  <p>I wrote a generic (and thread-safe) class for the deduplication:</p>
  
  <pre><code>public class Deduplication&lt;T&gt;
      where T : class
  {
      private static Deduplication&lt;T&gt; _global = new Deduplication&lt;T&gt;();
  
      public static Deduplication&lt;T&gt; Global
      {
          get { return _global; }
      }
  
      private Dictionary&lt;T, T&gt; _dic;// = new Dictionary&lt;T, T&gt;();
      private object _dicLocker = new object();
  
      public T GetInstance(T instance)
      {
          lock (_dicLocker)
          {
              if (_dic == null)
              {
                  _dic = new Dictionary&lt;T, T&gt;();
              }
  
              T savedInstance;
              if (_dic.TryGetValue(instance, out savedInstance))
              {
                  return savedInstance;
              }
              else
              {
                  _dic.Add(instance, instance);
                  return instance;
              }
          }
      }
  
      public void Clear()
      {
          lock (_dicLocker)
          {
              _dic = null;
          }
      }
  }
  </code></pre>
  
  <p>The problem with this class is that it adds a lot of more memory usage, and it stays there until the next GC.</p>
  
  <p>I searching for a way to reduce the memory footprint without adding a lot of more memory usage and without waiting for the next GC. Also i do not want to use <code>GC.Collect()</code> because it freezes the GUI for a couple of seconds.</p>