I have the following code:
IEnumerable<int> elements = Enumerable.Range(1, Int32.MaxValue);
Console.WriteLine("Size of System.Int32: {0}", sizeof(int));
Console.Write("Iterating over {0} elements... ", elements.Count());
Parallel.ForEach(elements, _ => { });
Console.WriteLine("Done.");
This produces:
> Size of System.Int32: 4
> Iterating over 2147483647 elements... Done.
However, I do not understand why this is not throwing away OutOfMemoryException
.
Knowing that each value int
occupies 4
space bytes, the allocation of the Int32.MaxValue
sum int
should take ~ 8 GB
Checking my application, the process takes aprox. ~ 5.200KB.
Elements are successfully repeated, so they should be highlighted somewhere, right?
How does LINQ achieve this?
source
share