Arraypool and memorypool instances used in C #

Time:2022-1-7

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 arrayYou only need to get it from the pool.

Using arraypool < T >

It can be used in the following three waysArrayPool<T>

  • adoptArrayPool<T>.SharedProperty to getArrayPool<T>example.
  • adoptArrayPool<T>.Create()To generateArrayPool<T>example.
  • By inheritanceArrayPool<T>To generate a self defined subclass.

The following code shows how to get one from arraypoolsize >= 10Array 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 2The 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.MemoryA 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>SoarrayRent out in the form of, andMemoryPool<T>SoMemory blockSo 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!

Recommended Today

IOS overall framework and class inheritance framework diagram

I. overall framework In the process of IOS development, understanding and learning the overall framework of IOS is an essential link. Today, let’s have a good understanding of the overall framework of IOS. First, post a framework introduction about IOS:Overview of IOS system framework。 We usually call the IOS framework cocoa touch framework. Cocoa touch […]