The best VPN 2024

The Best VPS 2024

The Best C# Book

How to use ArrayPool and MemoryPool in C#

How to use ArrayPool and MemoryPool in C#? The reusability of resources is a very important means to improve application performance. For example, the ArrayPool and MemoryPool to be shared in this article can effectively reduce memory usage and pressure on GC, thereby improving application performance.

How to choose WebClient HttpClient HttpWebRequest in .NET
How to use ArrayPool and MemoryPool

How to use ArrayPool and MemoryPool

System.Buffers the namespace provides a high-performance cell array of the class can be a multiplexed ArrayPool<T>, is often used in the array may be used scene ArrayPool<T>to reduce memory consumption, which 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 every new array will be allocated in the managed heap, GC also need to release the array when no longer in use Shihai, but ArrayPool<T>is born to resolve the matter, which dynamically maintains several array objects in the pool, when you need new array Just get it from the pool.

use ArrayPool<T>

It can be used in the following three ways ArrayPool<T>.

  • By ArrayPool<T>.Sharedacquiring attribute ArrayPool<T>instances.
  • By ArrayPool<T>.Create()generating ArrayPool<T>examples.
  • Inherited by ArrayPool<T>generating a self subclassing.

The following code shows how to obtain one from ArrayPool in size >= 10the array array.

var shared = ArrayPool<int>.Shared;
var rentedArray = shared.Rent(10);

How to use ArrayPool and MemoryPool
How to use ArrayPool and MemoryPool

The above code is important to note that although only 10 leased size, but the underlying returns Multiples of 2 the size, i.e. FIG. 2 * 8 = 16.

When rentedArray is no longer needed, remember to return it to ArrayPool, as shown in the following code.

shared.Return(rentedArray);

Below 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 implement a custom pooled object by overriding ArrayPool, 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();
        }
    }

use MemoryPool<T>

System.MemoryIt provides a pool of memory objects in the namespace MemoryPool<T>, before you need a new block of memory every time out, but also increase the burden on GC, with the MemoryPool<T>following, you need to take a block of memory from the pool directly on it.

        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();
        }

How to use ArrayPool and MemoryPool
How to use ArrayPool and MemoryPool

ArrayPool<T> vs MemoryPool<T>

As can be seen from the above presentation, ArrayPool<T>is arrayin the form of outwardly loan, and MemoryPool<T>is based on Memory block the way out lease, so in the scene may preferably reusable array ArrayPool<T>to improve performance, if your code is Memory<T>such a memory block The form of multiple use is preferred MemoryPool<T>.

Leave a Comment