LINQ creates many objects. Will it consume more memory?
PwC · Role not specified · technical
Published
Asked in 2026 · C#
Community answers
Posted by wasAsked editorial collection
LINQ can allocate iterator objects, delegates, closures, lookup structures, and result collections, but the amount depends on the operators and provider. Streaming operators such as `Where` and `Select` use deferred execution and usually process one element at a time. `ToList`, `ToArray`, `GroupBy`, `OrderBy`, and repeated enumeration can require more memory.
```csharp
IEnumerable<Order> expensive = orders
.Where(order => order.Total > 1_000m)
.Select(order => order); // deferred; no result list yet
foreach (var order in expensive)
Process(order);
```
Avoid accidental materialization and repeated pipelines:
```csharp
var summaries = orders
.Where(order => order.Total > 1_000m)
.Select(order => new OrderSummary(order.Id, order.Total))
.ToArray(); // intentional snapshot, enumerated once
```
In database queries, keep operations on `IQueryable` until the final projection so the server filters rows. For a measured hot loop, a `foreach`, span-based code, or value-oriented API may allocate less. Prefer clarity first, then benchmark representative data before replacing LINQ.
Pooja Parmar · · 0 likes
Yes, LINQ usually allocates more than a hand-written loop, but in most code the difference is small. It matters only in hot paths, which you should identify by measuring.
Where LINQ allocates
- Iterator objects: each operator (Where, Select, OrderBy) creates an enumerator object.
- Delegates and closures: lambdas that capture variables allocate a closure object and a delegate; non-capturing lambdas are cached.
- Interface enumeration: iterating via IEnumerable<T> can box struct enumerators (for example List<T>.Enumerator).
- Buffering operators: OrderBy, GroupBy, Reverse, Distinct and ToList build internal buffers or copies of the data.
- Materialisation: ToList() and ToArray() allocate new collections.
What has improved
- The .NET runtime has optimised LINQ heavily: specialised iterators for arrays and lists, combined Where/Select operators, and vectorised Sum, Min, Max and Average over arrays and spans. Many simple queries are close to loop performance in .NET 8–10.
- Count() and Any() avoid enumeration when the source knows its size (ICollection<T>).
When it matters
- Tight loops executed millions of times, high-throughput services where allocation rate drives GC pressure, or game and real-time code.
- Memory growth from materialising huge sequences unnecessarily (calling ToList() on millions of rows).
How to keep memory under control
- Avoid unnecessary ToList() calls; enumerate lazily when you only need one pass.
- Use Any() instead of Count() > 0; use FirstOrDefault instead of Where(...).ToList()[0].
- Avoid capturing variables in hot lambdas, or use static lambdas.
- For performance-critical code, use for or foreach loops over arrays or spans, or Span<T>-based APIs.
- With EF Core, keep queries as IQueryable so filtering happens in SQL, and project only needed fields — that saves far more memory than any LINQ-to-objects tuning.
How to decide
Profile with BenchmarkDotNet ([MemoryDiagnoser]) or dotnet-counters (allocation rate). If LINQ is not in the hot path, keep it for readability; correctness and clarity usually matter more than a few hundred bytes of allocation.