ASP.NET Core Interview Question #239
How would you troubleshoot high memory usage in an ASP.NET Core application?
Senior ASP.NET Core Real-World Scenarios Senior Advanced
Quick Interview Answer
Measure the process over time and determine whether memory is stable under load, grows because of legitimate caching, or continuously accumulates. Inspect GC metrics, allocation rates, large object heap activity, memory dumps, and profiler results.
Detailed Explanation
Measure the process over time and determine whether memory is stable under load, grows because of legitimate caching, or continuously accumulates. Inspect GC metrics, allocation rates, large object heap activity, memory dumps, and profiler results.
Common causes include unbounded caches or collections, large buffered payloads, retained object graphs, incorrect singleton lifetimes, undisposed unmanaged resources, excessive serialization, and loading too much database data at once.
Fix the retention or allocation source rather than forcing garbage collections, which usually treats the symptom rather than the cause.
Code Example
// Prefer bounded queries instead of loading entire tables.
var items = await db.Events
.AsNoTracking()
.OrderByDescending(x => x.Id)
.Take(100)
.ToListAsync(ct);