score:2

The MSDN for Enumerable.Select specifies that it "This method is implemented by using deferred execution. The immediate return value is an object that stores all the information that is required to perform the action." Hence, it allocates at least memory for the lambda (x=>x) and for the reference to the collection it is invoked on. However, this takes memory in the order of the complexity of the query and not in the order of the size of the collection and should therefore be mostly negligible.

Memory is only allocated in the order of the size of the collection when the result of the query is iterated. As Pareek's link shows, this usually happens once per query. In LINQ and Deferred Execution (even though it relates to LINQ to SQL), gives a good statement on this: If a LINQ method returns something different than IEnumerable it has to iterate the collection and may therefore allocate new memory (e.g. ToList(), ToArray()) but doesn't have to (e.g. Count()).

Hence, if you end up having some sort of collection in the end which is no IEnumerable, the query will allocate memory in the order of the size of this resulting collection.


Related Articles