Skip to content

Commit 9ccf5a4

Browse files
authored
Memory eviction - WN 10 P6 (#35676)
1 parent 9a3bfa4 commit 9ccf5a4

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

aspnetcore/release-notes/aspnetcore-10.0.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ For more information, see [ASP.NET Core Authorization and Authentication metrics
8888

8989
This section describes miscellaneous new features in .NET 10.
9090

91+
[!INCLUDE[](~/release-notes/aspnetcore-10/includes/memory-eviction.md)]
92+
9193
[!INCLUDE[](~/release-notes/aspnetcore-10/includes/httpsys.md)]
9294

9395
[!INCLUDE[](~/release-notes/aspnetcore-10/includes/testAppsTopLevel.md)]
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
### Automatic eviction from memory pool
2+
3+
The memory pools used by Kestrel, IIS, and HTTP.sys now 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+
#### Why memory eviction matters
6+
7+
Previously, 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.
8+
9+
#### Use memory eviction metrics
10+
11+
Metrics have been added to the default memory pool used by our server implementations. The new metrics are under the name `"Microsoft.AspNetCore.MemoryPool"`.
12+
13+
For information about metrics and how to use them, see <xref:log-mon/metrics/metrics>.
14+
15+
#### Manage memory pools
16+
17+
Besides using memory pools more efficiently by evicting unneeded memory blocks, .NET 10 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.
18+
19+
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:
20+
21+
```csharp
22+
public class MyBackgroundService : BackgroundService
23+
{
24+
private readonly MemoryPool<byte> _memoryPool;
25+
26+
public MyBackgroundService(IMemoryPoolFactory<byte> factory)
27+
{
28+
_memoryPool = factory.Create();
29+
}
30+
31+
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
32+
{
33+
while (!stoppingToken.IsCancellationRequested)
34+
{
35+
try
36+
{
37+
await Task.Delay(20, stoppingToken);
38+
// do work that needs memory
39+
var rented = _memoryPool.Rent(100);
40+
rented.Dispose();
41+
}
42+
catch (OperationCanceledException)
43+
{
44+
return;
45+
}
46+
}
47+
}
48+
}
49+
```
50+
51+
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:
52+
53+
```csharp
54+
services.AddSingleton<IMemoryPoolFactory<byte>,
55+
CustomMemoryPoolFactory>();
56+
57+
public class CustomMemoryPoolFactory : IMemoryPoolFactory<byte>
58+
{
59+
public MemoryPool<byte> Create()
60+
{
61+
// Return a custom MemoryPool implementation
62+
// or the default, as is shown here.
63+
return MemoryPool<byte>.Shared;
64+
}
65+
}
66+
```

aspnetcore/release-notes/sample/MemoryEviction.cs

Whitespace-only changes.

0 commit comments

Comments
 (0)