What's the point of c# memory pool?

Viewed 35

I get the concept of ArrayPools. But I don't quite understand why to use MemoryPools (C#). A Memory<T> represents a contiguous region of memory, like a (more complex) pointer. My thought is, that there is no need of pooling when directly pointing to an adress. Afaik nothing has to be allocated (except the overhead)? But the existence of this Struct indicates that there is a reason...so what is it? (googled/stackoverflowed but did not found sth. enlighting)

To make it plastic : What is the disadvantage in this use-scenario without an arraypool?

var arr = new double[100];
var mem = arr.AsMemory(0,100);

vs

var pool = MemoryPool<double>;
var owner = pool.Shared;
var arr = new double[100];
var mem = owner.Rent(100);
mem = arr.AsMemory(0,100);
1 Answers

A Memory<T> needs some memory to point to, and this memory needs to be allocated.

A MemoryPool is a place to rent pre-allocated memory. It is not an actual Memory<T> you are renting, but the memory it points to. Note that MemoryPool give you a IMemoryOwner<T>, i.e. an object that represents the actual ownership of the underlying memory, and you need to get your Memory<T> from this interface.

Objects larger than ~87kb (unless the limit has been changed) are allocated on the large object heap (LOH). This is only collected in gen 2 collections, that are much slower then gen 0 collections. The LOH is also not automatically defragmented, possibly leading till fragmentation issues if it is over used. Because of this it is recommended to use a memory pool when frequently allocating and deallocating large buffers.

So in your example it is of no advantage to use a memory pool. But it could for example be useful when working with hundreds of multi-megabyte images in a webserver.

Also, your example makes no sense, it should look something like

using var owner = MemoryPool<double>.Shared.Rent(100); // Use the singleton pool
Memmory<double> mem = owner.Memory;
Related