title: Linq2Sql out of memory exception
body: <p>I'm experiencing OutOfMemory exceptions in my application, when fetching from the database. It is a C# .Net application using Linq2Sql.</p>
  
  <p>I have tried using <code>GC.GetTotalMemory()</code> to see how much memory is taken up before and after the call to the database. This gives a nice although not quite accurate picture of what is going on. When I look in the Windows Task Manager I can see that the Peak Working Set is not smaller when fetching the data in a paged manner using the following code:</p>
  
  <pre><code>public static void PreloadPaged()
  {
      int NoPoints = PointRepository.Count();
      int pagesize = 50000;
      int fetchedRows = 0;
  
      while (fetchedRows &lt; NoPoints)
      {
          PreloadPointEntity.Points.AddRange(PointRepository.ReadPaged(pagesize, fetchedRows));
          PointRepository.ReadPointCollections();
          PreloadPointEntity.PointCollections.Count());
          fetchedRows += pagesize;
      }
  }
  
  
  private static List&lt;PointEntity&gt; ReadPaged(int pagesize, int fetchedRows)
  {
      DataModel dataContext = InstantiateDataModel();
      var Points = (from p in dataContext.PointDatas
                  select p.ToEntity());
  
      return Points.Skip(fetchedRows).Take(pagesize).ToList();
  }
  </code></pre>
  
  <p>I guess it's the Linq2Sql code that is using up the memory and not reusing it or freeing it afterwards, but what can I do to get the memory foot print down? </p>
  
  <p>I have observed that it uses 10 times as much memory to fetch the data as it does to store them in my list of enties. I have considered invoking the garbage collector, but I would rather avoid it.</p>