Streaming">
Streaming" loading="lazy" width="800" height="1000">
The Memory Trap of Materialization
During a recent performance audit of our reporting APIs, I tracked down erratic Gen2 GC pauses to a seemingly innocent endpoint. The code called .ToListAsync() on a massive table containing over 50,000 historical transaction records before returning an Ok(list) response.
This approach forces ASP.NET Core to allocate the entire result set in memory, doubling or tripling allocations during JSON serialization and locking up thread pool resources.
Before
[ HttpGet ( "reports" ) ] public async Task < IActionResult > GetReports ( ) { var list = await db . Orders . ToListAsync ( ) ; return Ok ( list ) ; // buffers all rows in RAM }
Enter fullscreen mode
Exit fullscreen mode
After
By leveraging IAsyncEnumerable<T> straight from Entity Framework Core, we bypass the intermediate list allocation entirely.…