Reusability of resources is a very important means to improve application performance. For example, arraypool and memorypool to be shared in this article effectively reduce memory usage and pressure on GC, so as to improve application performance.
What is arraypool
System. The buffers namespace provides a high-performance pooling class that can reuse arraysArrayPool<T>
, which can be used in scenes where array is often usedArrayPool<T>
To reduce memory consumption, it is an abstract class, as shown in the following code:
public abstract class ArrayPool<T>
{
}
Imagine that you need to instantiate array multiple times in your business scenario. What are the consequences of doing so? Obviously, each new array will be allocated on the managed heap. At the same time, when the array is no longer used, it needs to be released by GCArrayPool<T>
It is designed to solve this problem. It dynamically maintains several array objects in the pool when you need tonew array
You only need to get it from the pool.
Using arraypool < T >
It can be used in the following three waysArrayPool<T>
。
- adopt
ArrayPool<T>.Shared
Property to getArrayPool<T>
example. - adopt
ArrayPool<T>.Create()
To generateArrayPool<T>
example. - By inheritance
ArrayPool<T>
To generate a self defined subclass.
The following code shows how to get one from arraypoolsize >= 10
Array of.
var shared = ArrayPool<int>.Shared;
var rentedArray = shared.Rent(10);
The above code must be noted that although only 10 sizes are rented, the bottom layer will returnMultiple of 2
The size of, that is, 2 * 8 = 16 in the figure.
When you don’t need the rentedarray, remember to return it to the arraypool, as shown in the following code.
shared.Return(rentedArray);
The following is the complete code for reference only.
static void Main(string[] args)
{
var shared = ArrayPool<int>.Shared;
var rentedArray = shared.Rent(10);
for (int i = 0; i < 10; i++)
{
rentedArray[i] = i + 1;
}
for (int j = 0; j < 10; j++)
{
Console.WriteLine(rentedArray[j]);
}
shared.Return(rentedArray);
Console.ReadKey();
}
Create a custom arraypool
You can also override arraypool to implement custom pooled objects, as shown in the following code:
public class CustomArrayPool<T> : ArrayPool<T>
{
public override T[] Rent(int minimumLength)
{
throw new NotImplementedException();
}
public override void Return(T[] array, bool clearArray = false)
{
throw new NotImplementedException();
}
}
Using memorypool < T >
System.Memory
A memory pool object is provided under namespaceMemoryPool<T>
Before that, you need to create a new memory block every time, which also increases the burden of GCMemoryPool<T>
After that, you can take the memory blocks you need directly from the pool.
static void Main(string[] args)
{
var memoryPool = MemoryPool<int>.Shared;
var rentedArray = memoryPool.Rent(10);
for (int i = 0; i < 10; i++)
{
rentedArray.Memory.Span[i] = i + 1;
}
for (int j = 0; j < 10; j++)
{
Console.WriteLine(rentedArray.Memory.Span[j]);
}
Console.ReadKey();
}
ArrayPool<T>
vs MemoryPool<T>
As can be seen from the above demonstration,ArrayPool<T>
Soarray
Rent out in the form of, andMemoryPool<T>
SoMemory block
So it can be preferred in the scenario of reusing arrayArrayPool<T>
To improve performance if your code isMemory<T>
This memory block is preferred if it is used multiple timesMemoryPool<T>
This is the end of this article about using arraypool and memorypool instances in c# for more information about c# arraypool and memorypool, please search for previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!