📁 Sangam Dhakal - Portfolio v2.0 - Netscape Navigator 4.0
_
□
×

📚 Interview question

Memory keeps growing in your API and a gcdump shows thousands of objects rooted by ServiceProviderEngineScope. What is happening and how do you fix it?

Mid-Level Theory Intermediate dependency-injection dotnet-gcdump idisposable memory-leaks service-lifetimes

🎯 Key points

  • Each scope (including the root) keeps the IDisposable transients and scoped services it created until it is disposed
  • Retention path: ServiceProvider → root ServiceProviderEngineScope → disposables list → the leaked instances
  • Causes: resolving transient disposables from app.Services or an IServiceProvider injected into a singleton; a scope created once and never disposed
  • ValidateScopes does not catch it: resolving a transient from the root is legal
  • Fix options: IServiceScopeFactory.CreateAsyncScope() per operation (several scoped deps), or a factory whose result you dispose with using (one disposable)
  • Impact: growing Gen2 heap, longer full GCs, eventually OOM kills; verify the fix with dotnet-counters over hours

⏱ 30-second answer

The container is keeping objects alive because it promised to dispose them. Every scope, including the root, tracks the transient IDisposables it creates until the scope ends; for the root that means process shutdown. Typical causes are resolving from app.Services or from an IServiceProvider injected into a singleton (which is the root), or a scope created once and never disposed. ValidateScopes doesn't catch it.

Find the type and call site from the gcdump's retention path (ServiceProviderEngineScope → disposables), then either create a scope per operation with IServiceScopeFactory.CreateAsyncScope() when several scoped services are needed, or use a factory whose result you dispose with using when it's one object. The cost of leaving it: a growing Gen2 heap, longer full GCs and eventual OOM kills. Verify with dotnet-counters over several hours.

🔍 What the interviewer is checking

Checks they can recognise the root-tracked transient leak in a dump

↩ Usually asked after

More in Service Lifetimes

Last updated 23 Nov 2026.

🌐 Sangam's Webring

⏪ Previous 🏠 Main Next ⏩
💬 Chat with me