RabbitMQ.Client library uses ArrayPool<T>.Shared memory pool to arrange the memory for internal activities. On .NET 6 due to changes in this type of pool I'm observing high memory consumption for the code utilizing BasicPublish method. This is happening when the BasicPublish has a higher rate of invocation than ArrayPool.Return inside of WriteLoop. This is well known issue of shared ArrayPool when Return is called of a different thread than Rent. Also, it is known that BasicPublish copies the supplied ReadOnlyMemory<byte> using shared ArrayPool, even if the caller of this method uses different memory pooling mechanism.
I would like to suggest a way to override memory pooling mechanism for RabbitMQ.Client internals. Actually, we can have two ways to do that:
1. Add a property of type MemoryPool<T> to IConnection. This allows a user of the library to override the allocation mechanism. However, MemoryPool<T>.Rent returns IMemoryOwner<T> that requires extra allocation of a thin wrapper around the memory block.
2. If standard base class is not an option due to small allocation, it is possible to offer custom interface that allows to manage allocations.
The interface can have the following design:
interface IMemoryAllocator
{
Memory<byte> Rent(int minimumSize);
void Return(Memory<byte> memory);
}