Inefficient memory pooling (.NET client)

135 views
Skip to first unread message

roman...@gmail.com

unread,
Mar 30, 2022, 10:18:41 AM3/30/22
to rabbitmq-users
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);
}

Luke Bakken

unread,
Mar 31, 2022, 8:34:53 AM3/31/22
to rabbitmq-users
Hello,

Please open an issue to explain the issue and your request here - https://github.com/rabbitmq/rabbitmq-dotnet-client

If you'd like to implement the feature on the "main" and "6.x" branch that would be great. Pull requests would be appreciated.

Thanks,
Luke

Luke Bakken

unread,
Mar 31, 2022, 8:35:58 AM3/31/22
to rabbitmq-users
Also, code that clearly demonstrates this issue will be required so that we can prove the issue exists and is resolved with a custom memory allocator.

Thanks
Luke
Reply all
Reply to author
Forward
0 new messages