title: why am I getting a System.OutOfMemoryException
body: <p>I am using Linq to Sql.
  Here is the code:</p>
  
  <pre><code>Dictionary&lt;string, int&gt; allResults;
  using (var dc= new MyDataContext())
  {
      dc.CommandTimeout = 0;
      allResults = dc.MyTable.ToDictionary(x =&gt; x.Text, x =&gt; x.Id);
  }
  </code></pre>
  
  <p>it is ran on a 64 bit machine and the compilation is AnyCPU. It throws a System.OutOfMemoryException.</p>
  
  <p>This accesses an SQL Server database. The Id field maps to an SQL Server int field, And the Text field maps to Text(nvarchar(max)) field. Running <code>select COUNT(*) from TableName</code> results in 1,173,623 records and running <code>select sum(len(Text)) from TableName</code> results in 48,915,031. Since int is a 32 bit integer, the ids should take only 4.69MB of space and the strings less than 1GB. So we are not even bumping against the 2GB/object limit.</p>
  
  <p>I then change the code in this way:</p>
  
  <pre><code>Dictionary&lt;string, int&gt; allResults;
  using (var dc = new MyDataContext())
  {
     Dictionary&lt;string, int&gt; threeHundred;
     dc.CommandTimeout = 0;
     var tot = dc.MyTable.Count();
     allResults = new Dictionary&lt;string, int&gt;(tot);
     int skip = 0;
     int takeThis = 300000;
     while (skip &lt; tot)
     {
        threeHundred = dc.MyTable.Skip(skip).Take(takeThis).ToDictionary(x =&gt; x.Text, x =&gt; x.Id);
        skip = skip + takeThis;
        allResults = allResults.Concat(threeHundred).ToDictionary(x =&gt; x.Key, x =&gt; x.Value);
        threeHundred = null;
        GC.Collect();
      }
  }
  </code></pre>
  
  <p>I learn that garbage collaction here does not help and that the out of memory exception is thrown on the first line in the while loop once skip = 900,000.</p>
  
  <p>What is wrong and how do I fix this?</p>