C# Entity Framework 6: Out Of Memory when it absolutely should not run out of memory
<p>I've simplified things as much as possible. This is reading from a table that has around 3,000,000 rows. I want to create a Dictionary from some concatenated fields of the data.</p>

<p>Here's the code that, in my opinion, should never, ever throw an Out Of Memory Exception:</p>

<pre><code>public int StupidFunction()
{
    var context = GetContext();
    int skip = 0;
    int take = 100000;
    var batch = context.VarsHG19.OrderBy(v =&gt; v.Id).Skip(skip).Take(take);
    while (batch.Any())
    {
        batch.ToList();
        skip += take;
        batch = context.VarsHG19.OrderBy(v =&gt; v.Id).Skip(skip).Take(take);
    }

    return 1;
}
</code></pre>

<p>In my opinion, the batch object should simply be replaced each iteration and the previous memory allocated for the previous batch object should be garbage collected. I would expect that the loop in this function should take a nearly static amount of memory. At the very worst, it should be bounded by the memory needs of one row * 100,000. The Max size of a row from this table is 540 bytes. I removed Navigation Properties from the edmx.</p>