Skip to content

Commit 98aa18a

Browse files
committed
draft
1 parent 6a01f80 commit 98aa18a

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

aspnetcore/fundamentals/servers/httpsys.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
## Automatic eviction from memory poools
2+
3+
The memory pools used by Kestrel, IIS, and HTTP.sys automatically evict memory blocks when the application is idle or under less load. The feature runs automatically and doesn't need to be enabled or configured manually.
4+
5+
In versions of .NET earlier than 10 earlier than 10, memory allocated by the pool would remain reserved, even when not in use. This feature releases memory back to the system when the app is idle for a period of time. This eviction 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 improves the experience of creating memory pools. It does this by providing a built-in [IMemoryPoolFactory](https://source.dot.net/#Microsoft.AspNetCore.Connections.Abstractions/IMemoryPoolFactory.cs) and a `MemoryPoolFactory` 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+
```csharp
20+
public class MyBackgroundService : BackgroundService
21+
{
22+
private readonly MemoryPool<byte> _memoryPool;
23+
24+
public MyBackgroundService(IMemoryPoolFactory<byte> factory)
25+
{
26+
_memoryPool = factory.Create();
27+
}
28+
29+
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
30+
{
31+
while (!stoppingToken.IsCancellationRequested)
32+
{
33+
try
34+
{
35+
await Task.Delay(20, stoppingToken);
36+
// do work that needs memory
37+
var rented = _memoryPool.Rent(100);
38+
rented.Dispose();
39+
}
40+
catch (OperationCanceledException)
41+
{
42+
return;
43+
}
44+
}
45+
}
46+
}
47+
```
48+
49+
To use your own 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:
50+
51+
```csharp
52+
services.AddSingleton<IMemoryPoolFactory<byte>,
53+
CustomMemoryPoolFactory>();
54+
55+
public class CustomMemoryPoolFactory : IMemoryPoolFactory<byte>
56+
{
57+
public MemoryPool<byte> Create()
58+
{
59+
// Return a custom MemoryPool implementation
60+
// or the default, as is shown here.
61+
return MemoryPool<byte>.Shared;
62+
}
63+
}
64+
```

aspnetcore/fundamentals/servers/kestrel.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 memory pool eviction.
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>

0 commit comments

Comments
 (0)