aspnetcore/fundamentals/servers/kestrel/memory-management.md
By Tom Dykstra
This article provides guidance for managing memory in Kestrel, including automatic eviction from memory pools and using memory pool metrics.
The memory pools used by Kestrel, IIS, and HTTP.sys automatically evict memory blocks when the application is idle or under low load. The feature runs automatically and doesn't need to be enabled or configured manually.
This automatic eviction feature reduces overall memory usage and helps applications stay responsive under varying workloads. In versions of .NET earlier than 10, memory allocated by the pool remained reserved even when not in use.
The default memory pool used by the ASP.NET Core server implementations includes metrics, which can be used to monitor and analyze memory usage patterns. The metrics are under the name "Microsoft.AspNetCore.MemoryPool".
For information about metrics and how to use them, see xref:log-mon/metrics/metrics.
Besides using memory pools efficiently by evicting unneeded memory blocks, ASP.NET Core provides a built-in IMemoryPoolFactory interface and its default implementation, which are available through dependency injection.
The following code example shows a simple background service that uses the built-in memory pool factory implementation to create memory pools. These pools benefit from the automatic eviction feature:
:::code language="csharp" source="~/fundamentals/servers/snippets/10.x/my-background-service.cs":::
To use a custom memory pool factory, make a class that implements IMemoryPoolFactory and register it with dependency injection, as the following example does. Memory pools created this way do not benefit from the automatic eviction feature unless you implement similar eviction logic in your custom factory:
:::code language="csharp" source="~/fundamentals/servers/snippets/10.x/memory-pool-factory.cs":::
When you're using a memory pool, be aware of the pool's xref:System.Buffers.MemoryPool`1.MaxBufferSize.