Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would OutOfMemoryException be thrown while using PLINQ Take()?

After I upgraded to DotNet 4.5, a query started giving me OutOfMemoryExceptions.

The (distilled) query is:

var tests = new int[]{}
    .AsParallel()
    .GroupBy(_ => _)
    .Take(int.MaxValue)
    .ToArray();

I'm posting this for anyone with the same problem. I'll answer below.

like image 282
Pablo Montilla Avatar asked Aug 16 '12 20:08

Pablo Montilla


1 Answers

It appears to be a change in the framework.

The Take() operator is implemented in the TakeOrSkipQueryOperator internal class. There is one branch in the code that goes through a WrapHelper() function that creates a FixedMapHeap instance which in turn creates an array of Key elements of the size passed originally to Take() (which would be an array of 8Gb in the given example).

like image 144
Pablo Montilla Avatar answered Oct 09 '22 15:10

Pablo Montilla