title: Are MakeGenericType / generic types garbage collected?
body: <p>It is well known in .NET that types are not garbage collected, which means that if you're playing around with f.ex. Reflection.Emit, you have to be careful to unload AppDomains and so on... At least that's how I used to understand how things work.</p>
  
  <p>That made me wonder if generic types <em>are</em> garbage collected, to be more precise: generics created with <code>MakeGenericType</code>, let's say... for example based on user input. :-)</p>
  
  <p>So I constructed the following test case:</p>
  
  <pre><code>public interface IRecursiveClass
  {
      int Calculate();
  }
  
  public class RecursiveClass1&lt;T&gt; : IRecursiveClass 
                                    where T : IRecursiveClass,new()
  {
      public int Calculate()
      {
          return new T().Calculate() + 1;
      }
  }
  public class RecursiveClass2&lt;T&gt; : IRecursiveClass
                                    where T : IRecursiveClass,new()
  {
      public int Calculate()
      {
          return new T().Calculate() + 2;
      }
  }
  
  public class TailClass : IRecursiveClass
  {
      public int Calculate()
      {
          return 0;
      }
  }
  
  class RecursiveGenericsTest
  {
      public static int CalculateFromUserInput(string str)
      {
          Type tail = typeof(TailClass);
          foreach (char c in str)
          {
              if (c == 0)
              {
                  tail = typeof(RecursiveClass1&lt;&gt;).MakeGenericType(tail);
              }
              else
              {
                  tail = typeof(RecursiveClass2&lt;&gt;).MakeGenericType(tail);
              }
          }
          IRecursiveClass cl = (IRecursiveClass)Activator.CreateInstance(tail);
          return cl.Calculate();
      }
  
      static long MemoryUsage
      {
          get
          {
              GC.Collect(GC.MaxGeneration);
              GC.WaitForFullGCComplete();
              return GC.GetTotalMemory(true);
          }
      }
  
      static void Main(string[] args)
      {
          long start = MemoryUsage;
  
          int total = 0;
          for (int i = 0; i &lt; 1000000; ++i)
          {
              StringBuilder sb = new StringBuilder();
              int j = i;
              for (int k = 0; k &lt; 20; ++k) // fix the recursion depth
              {
                  if ((j &amp; 1) == 1)
                  {
                      sb.Append('1');
                  }
                  else
                  {
                      sb.Append('0');
                  }
                  j &gt;&gt;= 1;
              }
  
              total += CalculateFromUserInput(sb.ToString());
  
              if ((i % 10000) == 0)
              {
                  Console.WriteLine("Current memory usage @ {0}: {1}", 
                                    i, MemoryUsage - start);
              }
          }
  
          Console.WriteLine("Done and the total is {0}", total);
          Console.WriteLine("Current memory usage: {0}", MemoryUsage - start);
  
          Console.ReadLine();
      }
  }
  </code></pre>
  
  <p>As you can see, the generic types are defined 'possibly recursive', with a 'tail' class that marks the end of the recursion. And to ensure that <code>GC.TotalMemoryUsage</code> isn't cheating, I also opened Task Manager.</p>
  
  <p>So far so good. Next thing I did was fire this beast up and while I was waiting for an 'Out of memory' ... I noticed that it was - contrary to my expectations - <strong>not</strong> consuming more memory over time. In fact, it shows a slight drop in memory consumption in time.</p>
  
  <p>Can someone please explain this? Are generic types actually collected by the GC? And if so... are there also Reflection.Emit cases that are garbage collected?</p>