Skip to content

Commit 8f0edaf

Browse files
Automatic eviction from memory pool - Kestrel and HTTP.sys (#35822)
* Update aspnetcore/fundamentals/servers/includes/memory-eviction2.md * ms.date * Change link to xref link * Change date to trigger build of include * check MaxBufferSize Co-authored-by: Brennan <[email protected]> --------- Co-authored-by: Brennan <[email protected]>
1 parent ad0b6c0 commit 8f0edaf

File tree

6 files changed

+78
-4
lines changed

6 files changed

+78
-4
lines changed

aspnetcore/fundamentals/servers/httpsys.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: Learn about HTTP.sys, a web server for ASP.NET Core on Windows. Bui
55
monikerRange: '>= aspnetcore-2.1'
66
ms.author: tdykstra
77
ms.custom: mvc
8-
ms.date: 06/13/2025
8+
ms.date: 08/04/2025
99
uid: fundamentals/servers/httpsys
1010
---
1111
# HTTP.sys web server implementation in ASP.NET Core
@@ -32,6 +32,7 @@ HTTP.sys supports the following features:
3232
* Response caching
3333
* WebSockets (Windows 8 or later)
3434
* Customizable security descriptors
35+
* Automatic memory pool eviction
3536

3637
Supported Windows versions:
3738

@@ -340,6 +341,8 @@ Requirements to run gRPC with HTTP.sys:
340341

341342
For information about how to get traces from HTTP.sys, see [HTTP.sys Manageability Scenarios](/windows/win32/http/http-sys-manageability-scenarios).
342343

344+
[!INCLUDE[](includes/memory-eviction2.md)]
345+
343346
## Additional resources
344347

345348
* [Enable Windows Authentication with HTTP.sys](xref:security/authentication/windowsauth#httpsys)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
## Automatic eviction from memory pool
2+
3+
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.
4+
5+
In versions of .NET earlier than 10, memory allocated by the pool remains reserved, even when not in use. This automatic eviction feature reduces overall memory usage and helps applications stay responsive under varying workloads.
6+
7+
### Use memory pool metrics
8+
9+
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"`.
10+
11+
For information about metrics and how to use them, see <xref:log-mon/metrics/metrics>.
12+
13+
## Manage memory pools
14+
15+
Besides using memory pools efficiently by evicting unneeded memory blocks, ASP.NET Core provides a built-in [IMemoryPoolFactory](https://source.dot.net/#Microsoft.AspNetCore.Connections.Abstractions/IMemoryPoolFactory.cs) and an implementation. It makes the implementation available to your application through dependency injection.
16+
17+
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:
18+
19+
:::code language="csharp" source="~/fundamentals/servers/snippets/10.x/my-background-service.cs":::
20+
21+
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 also benefit from the automatic eviction feature:
22+
23+
:::code language="csharp" source="~/fundamentals/servers/snippets/10.x/memory-pool-factory.cs":::
24+
25+
When you're using a memory pool, be aware of the pool's <xref:System.Buffers.MemoryPool`1.MaxBufferSize>.
26+

aspnetcore/fundamentals/servers/kestrel.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: Learn about Kestrel, the cross-platform web server for ASP.NET Core
55
monikerRange: '>= aspnetcore-3.1'
66
ms.author: tdykstra
77
ms.custom: mvc
8-
ms.date: 04/04/2023
8+
ms.date: 08/04/2023
99
uid: fundamentals/servers/kestrel
1010
---
1111
# Kestrel web server in ASP.NET Core
@@ -14,7 +14,7 @@ uid: fundamentals/servers/kestrel
1414

1515
By [Tom Dykstra](https://github.com/tdykstra), [Chris Ross](https://github.com/Tratcher), and [Stephen Halter](https://twitter.com/halter73)
1616

17-
:::moniker range=">= aspnetcore-8.0"
17+
:::moniker range=">= aspnetcore-10.0"
1818

1919
Kestrel is a cross-platform [web server for ASP.NET Core](xref:fundamentals/servers/index). Kestrel is the recommended server for ASP.NET Core, and it's configured by default in ASP.NET Core project templates.
2020

@@ -33,6 +33,7 @@ Kestrel's features include:
3333
* Building a reverse proxy with [YARP](https://github.com/microsoft/reverse-proxy).
3434
* **Extensibility:** Customize Kestrel through configuration, middleware, and custom transports.
3535
* **Performance diagnostics:** Kestrel provides built-in performance diagnostics features, such as logging and metrics.
36+
* **Memory management:** Kestrel includes features for efficient memory management, such as automatic eviction from memory pool.
3637

3738
## Get started
3839

@@ -58,6 +59,9 @@ The following timeouts and rate limits aren't enforced when a debugger is attach
5859
* <xref:Microsoft.AspNetCore.Server.Kestrel.Core.Features.IHttpMinRequestBodyDataRateFeature>
5960
* <xref:Microsoft.AspNetCore.Server.Kestrel.Core.Features.IHttpMinResponseDataRateFeature>
6061

62+
63+
[!INCLUDE[](includes/memory-eviction2.md)]
64+
6165
## Additional resources
6266

6367
<a name="endpoint-configuration"></a>

aspnetcore/fundamentals/servers/kestrel/includes/kestrel6.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
:::moniker range=">= aspnetcore-6.0 < aspnetcore-8.0"
1+
:::moniker range=">= aspnetcore-6.0 <=aspnetcore-9.0"
22

33
Kestrel is a cross-platform [web server for ASP.NET Core](xref:fundamentals/servers/index). Kestrel is the web server that's included and enabled by default in ASP.NET Core project templates.
44

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
services.AddSingleton<IMemoryPoolFactory<byte>,
2+
CustomMemoryPoolFactory>();
3+
4+
public class CustomMemoryPoolFactory : IMemoryPoolFactory<byte>
5+
{
6+
public MemoryPool<byte> Create()
7+
{
8+
// Return a custom MemoryPool implementation
9+
// or the default, as is shown here.
10+
return MemoryPool<byte>.Shared;
11+
}
12+
}
13+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
public class MyBackgroundService : BackgroundService
2+
{
3+
private readonly MemoryPool<byte> _memoryPool;
4+
5+
public MyBackgroundService(IMemoryPoolFactory<byte> factory)
6+
{
7+
_memoryPool = factory.Create();
8+
}
9+
10+
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
11+
{
12+
while (!stoppingToken.IsCancellationRequested)
13+
{
14+
try
15+
{
16+
await Task.Delay(20, stoppingToken);
17+
// do work that needs memory
18+
// consider checking _memoryPool.MaxBufferSize
19+
var rented = _memoryPool.Rent(100);
20+
rented.Dispose();
21+
}
22+
catch (OperationCanceledException)
23+
{
24+
return;
25+
}
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)