title: Avoiding memory leak from a list returned by an entity framework query
body: <p>I would like to know how I can ensure that memory allocated to a list of objects will be freed.</p>
  
  <p>I have a method in a c# .NET program which returns a list of objects filtered depending upon a number of criteria. The method is written so that this filtering cannot be performed all in one query therefore it keeps recreating the list as the filter is refined.</p>
  
  <p>I strongly believe that this method is responsible for a memory as after several calls to this method the users are reporting a crash with an Out of Memory Exception.</p>
  
  <p>The code is like this:</p>
  
  <pre><code>    private List&lt;Things&gt; GetMatchingThings(DataContext context, string number, 
                                         DateTime startDate, DateTime endDate, 
                                         string otherNumber, string orderNumber, 
                                         string shiftNumber, 
                                         bool includeDeletedThings)
  {
  
      List&lt;Things&gt; thingList =  context.Set&lt;Things&gt;()  
                                       .Where(th =&gt; th.Number == number &amp;&amp;
                                              th.FinishDateTime.HasValue &amp;&amp; 
                                              !th.IsRunning)  
                                       .ToList();
  
      if (thingList != null &amp;&amp; thingList.Count &gt; 0 )
      {
            var filteredList = thingList.FindAll(th =&gt; th.StartDateTime.Date &gt;= startDate.Date 
                                                       &amp;&amp; 
                                                       th.StartDateTime.Date &lt;= endDate.Date);
            thingList = filteredList;
      }
  
      if (thingList != null &amp;&amp; thingList.Count &gt; 0 &amp;&amp; otherNumber.IsNotNullOrEmpty())
      {
            var filteredList = thingList.FindAll(th =&gt; th.OtherNumber.Equals(otherNumber));
            thingList = filteredList;
      }
  
      if (thingList != null &amp;&amp; thingList.Count &gt; 0 &amp;&amp; orderNumber.IsNotNullOrEmpty())
      {
            var filteredList = thingList.FindAll(th =&gt; th.orderNumber != null &amp;&amp;
                                              th.orderNumber.Equals(orderNumber));
            thingList = filteredList;
      }
  
      // Other repetitions of the filtering above for the remaining search criteria
  
      if (thingList != null &amp;&amp; thingList.Count &gt; 0)
      {
                  thingList.Sort((x,y) =&gt; y.StartDateTime.CompareTo(x.StartDateTime));
      }
  
      return thingList;
      }
  </code></pre>
  
  <p>The filtering is done in stages like this because some of the search criteria are optional, only the start date will always be included. So the initial list is found and then filtered further depending upon which other criteria have been provided.</p>
  
  <p>However, I am assuming that the FindAll method calls used create a new list but then when this new list is assigned to thingList the original memory occupied by thingList is not being reclaimed.</p>
  
  <p>As far as I know calling thingList.Clear() after each new filteredList is created would not help since Clear just removed items from the list but does not reclaim the memory.</p>
  
  <p>The Thing object also contains a property which is unmanaged.</p>
  
  <p>My question is how can I</p>
  
  <p>a) Find a way to free the memory allocated to thingList and filteredList in the intermediate queries</p>
  
  <p>or</p>
  
  <p>b) Rewrite the original Where query to do this all in one go given that some of the search criteria may not be needed. Is there a way of using a wildcard in a Query like this as there is in SQL, e.g. .Where th.orderNumber.Equals("%")</p>