How to use ArrayPool and MemoryPool in C#

Spread the love

Take advantage of array pooling and memory pooling in C# to reduce allocations and improve the performance of your applications

How to use ArrayPool and MemoryPool in C#
Thinkstock

Optimal usage of available resources is one of the most important strategies for improving application performance. By using the ArrayPool and MemoryPool classes in C#, you can minimize memory allocations and garbage collection overhead and hence improve performance.

This article discusses these resource, memory, and object pooling mechanisms and how to work with them in C#. To work with the code examples provided in this article, you should have Visual Studio 2019 installed in your system. If you don’t already have a copy, you can download Visual Studio 2019 here.

Create a .NET Core console application project in Visual Studio

First off, let’s create a .NET Core console application project in Visual

Studio. Assuming Visual Studio 2019 is installed in your system, follow the steps outlined below to create a new .NET Core console application project in Visual Studio.

  1. Launch the Visual Studio IDE.
  2. Click on “Create new project.”
  3. In the “Create new project” window, select “Console App (.NET Core)” from the list of templates displayed.
  4. Click Next.
  5. In the “Configure your new project” window shown next, specify the name and location for the new project.
  6. Click Create.

This will create a new .NET Core console application project in Visual Studio 2019. We’ll use this project to work with ArrayPool and MemoryPool in the subsequent sections of this article.

What is ArrayPool? Why is it needed?

The ArrayPool<T> class in the System.Buffers namespace is a high-performance pool of reusable managed arrays. It can be used to minimize allocations and improve performance in cases where arrays are often reused. The ArrayPool<T> class is defined as an abstract class as shown in the code snippet that follows:

public abstract class ArrayPool<T>

Imagine a situation where you must create instances of an array several times. This will result in overhead for the garbage collector because memory needs to be allocated when the array is created and then deallocated when the array is no longer needed.

Here’s exactly where an ArrayPool<T> can help conserve resources. You can take advantage of an ArrayPool to reserve some arrays and then rent them out in a thread-safe manner when asked for. An ArrayPool is a good choice whenever you’re having to repeatedly create and destroy arrays in your code.

Use the ArrayPool<T> class in C#

You can use the ArrayPool<T> class in the following three ways:

  • Using the ArrayPool<T>.Shared property to get a shared ArrayPool<T> instance
  • Using the static ArrayPool<T>.Create method to create a new instance of ArrayPool<T>
  • Creating a custom ArrayPool class by extending ArrayPool<T>

The following code snippet illustrates how you can rent an array from the ArrayPool.

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

In the above example, the integer array named rentedArray will have 10 elements; i.e., you can store 10 integer values in the array.

You can also write the preceding code as shown below.

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

To return the array back to the pool, you should call the Return method as follows.

shared.Return(rentedArray);

Here is the complete code listing for your reference:

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

The ArrayPool<T>.Create method can be used to create a new instance of the ArrayPool<T> class. The following code snippet provides an example.

var arrayPool = ArrayPool<int>.Create(4, 10);
var rentedArray = arrayPool.Rent(10);

Create a custom ArrayPool class in C#

You can create your own implementation of ArrayPool, i.e., a custom array pool class as shown below.

 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 the MemoryPool<T> class in C#

The System.Buffers.MemoryPool<T> class pertaining to the System.Memory namespace represents a memory pool. MemoryPool<T> is a good choice when your code needs to allocate blocks of memory and you would like to reduce the pressure on the GC by reusing the allocated memory rather than creating new memory blocks each time.

The following code snippet illustrates how you can work with memory blocks. We’ll create a memory pool and then rent a memory block from it.

static void Main(string[] args)
{
     var memoryPool = MemoryPool<int>.Shared;
     var arrayPool = ArrayPool<int>.Create(4, 10);
     var rentedArray = arrayPool.Rent(10);
     for (int i=0; i < 10; i++)
     {
           rentedArray[i] = i + 1;
     }
     for(int j=0;j < 10; j++)
     {
        Console.WriteLine(rentedArray[j]);
     }
     arrayPool.Return(rentedArray);
     Console.ReadKey();
}

When you execute the above program, the numbers 1 to 10 will be displayed at the console window.

ArrayPool<T> vs. MemoryPool<T>

The ArrayPool<T> class rents out arrays using the Shared property, while the MemoryPool<T> class rents out IMemoryOwner<T> implementations. You should use ArrayPool<T> if you need to create array instances repeatedly. And you should use MemoryPool<T> if you’re working with Memory<T> instances. Memory pools are used to reuse existing memory blocks; you can use them to allocate memory blocks dynamically. Array pools manage a pool of arrays and rent them when asked for.

Finally, keep in mind that object pooling can also be used to reduce resource overhead by recycling objects rather than recreating them each time they’re needed. You can learn more about object pools and the object pool design pattern from my earlier article here.

Join Geezgo for free. Use Geezgo\’s end-to-end encrypted Chat with your Closenets (friends, relatives, colleague etc) in personalized ways.>>

 202 

You may also like...

Leave a Reply