Description
Background and Motivation
Kestrel (and other server impls) use the PinnedBlockMemoryPool for the core IO processing. The usage is purely internal today and also has the issue of never releasing its memory resulting in users thinking there is a memory leak. See: #55490
As part of wanting to reduce the memory when the pool is idle we want to use a timer, but creating a timer per pool is not ideal, so we want to put the cleanup on the kestrel heartbeat thread which requires sharing between Sockets, NamedPipes, and Kestrel Core. Ideally we want to avoid internals visible to, so in order to solve this we need a type that can be used by all dlls and shared via DI so we can wire up the pool usage everywhere.
Proposed API
Microsoft.AspNetCore.Connections.Abstractions.dll
namespace Microsoft.AspNetCore.Connections;
+ public interface IMemoryPoolFactory
+ {
+ MemoryPool<byte> CreatePool();
+ }
Usage Examples
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSingleton<IMemoryPoolFactory, MyMemoryPoolFactory>();
var app = builder.Build();
public sealed class MyMemoryPoolFactory : IMemoryPoolFactory
{
public MemoryPool<byte> CreatePool()
{
return MemoryPool<byte>.Shared;
}
}
Alternative Designs
Alternatively we can use [InternalsVisibleTo]
on some of our assemblies.
Risks
N/A
Activity
dotnet-policy-service commentedon Mar 18, 2025
Thank you for submitting this for API review. This will be reviewed by @dotnet/aspnet-api-review at the next meeting of the ASP.NET Core API Review group. Please ensure you take a look at the API review process documentation and ensure that:
halter73 commentedon Mar 20, 2025
API Review Notes:
SocketTransportOptions.MemoryPoolFactory
IMemoryPoolFeature
is unwieldy for that.CreatePool
?GetPool
?Create
?GetOrCreate
?CreateMemoryPool
?Create
!ObjectPoolProvider<T>
.API Approved!
Microsoft.AspNetCore.Connections.Abstractions.dll