title: C# not releasing memory after task complete
body: <p>The following code is a simplified example of an issue I am seeing. This application consumes approx 4GB of memory before throwing an exception as the dictionary is too big.</p>
  
  <pre><code> class Program
   {
      static void Main(string[] args)
      {
          Program program = new Program();
  
          while(true)
          {
              program.Method();
              Console.ReadLine();
          }
      }
  
      public void Method()
      {
          WasteOfMemory memory = new WasteOfMemory();
          Task tast = new Task(memory.WasteMemory);
          tast.Start();
      }
  
  
  }
  
  public class WasteOfMemory
  {
       public void WasteMemory()
       {
           Dictionary&lt;string, string&gt; aMassiveList = new Dictionary&lt;string, string&gt;();
  
           try
           {
               long i = 0;
               while (true)
               {
                   aMassiveList.Add(i.ToString(), "I am a line of text designed to waste space.... I am exceptionally useful........");
                   i++;
               }
  
           }
           catch(Exception e)
           {
               Console.WriteLine("I have broken myself");
           }
       }
  }
  </code></pre>
  
  <p>This is all as expected, although what we cannot currently work out is when this memory should be released from the CLR. </p>
  
  <p>We have let the task complete and then simulated a memory overload situation, but the memory consumed by the dictionary is not released. As the OS is running out of memory, is it not putting pressure on the CLR to release the memory?</p>
  
  <p>However and even more confusing, if we wait until the task has completed, then hit enter to run the task again the memory is released, so obviously the previous dictionary has been garbage collected (hasn't it?).</p>
  
  <p>So, why is the memory not being released? And how can we get the CLR to release the memory? </p>
  
  <p>Any explanations or solutions would be greatly appreciated.</p>
  
  <p>EDIT: Following replies, particularly Beska's, it is obvious my description of the issue is not the the clearest, so I will try to clarify.</p>
  
  <p>The code may not be the best example, sorry! It was a quick crude piece of code to try to replicate the issue.</p>
  
  <p>The dictionary is used here to replicate the fact we have a large custom data object, which fills a large chunk of our memory and it is not then released after the task has completed.</p>
  
  <p>In the example, the dictionary fills up to the limit of the dictionary and then throws an exception, it does NOT keep filling forever! This is well before our memory is full, and it does not cause an OutOfMemoryException. Hence the result is a large object in memory, and then the task completes.</p>
  
  <p>At this point we would expect the dictionary to be out of scope, as both the task and the method 'Method' have completed. Hence, we would expect the dictionary to be garbage collected and the memory reclaimed. In reality, the memory is not freed until 'Method' is called again, creating a new WasteOfMemory instance and starting a new task.</p>
  
  <p>Hopefully that will clarify the issue a bit</p>