Enumerable.Range and Memory allocation

梦想的初衷 提交于 2020-01-01 11:58:46

问题


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 prints out:

> Size of System.Int32: 4
> Iterating over 2147483647 elements... Done.

However, I don't understand why is this not throwing an OutOfMemoryException.

Knowing that each int value takes up 4 bytes of space, allocating Int32.MaxValue amount of int should take up ~ 8GB

Inspecting my application, the process is taking up aprox. ~ 5.200KB.

The elements are being iterated successfully so they must be allocated somewhere, right?

How does LINQ achieve this?


回答1:


IEnumerable<int> is not an array. It doesn't store any information itself. The class implementing it is capable of looping over a set of values.

The values here are not stored in an array, instead of that is just iterates and yields the result of every iteration.

Something like this:

public IEnumerable<int> GetRange(int start, int number)
{
    for (int i = start; i < start + number; i++)
    {
        yield return i;
    }
}

See, no arrays, no storage. It just remembers the current position in the iterator and can execute the appropriate steps to get the next one. This code is generated by the C# compiler on the fly.



来源:https://stackoverflow.com/questions/33217435/enumerable-range-and-memory-allocation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!