joydip_kanjilal
Contributor

How to use ArrayPool and MemoryPool in C#

how-to
Nov 09, 20207 mins
C#Microsoft .NETSoftware Development

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

swimming pool 168812304
Credit: 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 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 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 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 class in C#

You can use the ArrayPool class in the following three ways:

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

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.Shared;
            var rentedArray = shared.Rent(10);
            for (int i=0; i shared.Return(rentedArray);
            Console.ReadKey();
        }

The ArrayPool.Create method can be used to create a new instance of the ArrayPool 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 class in C#

The System.Buffers.MemoryPool class pertaining to the System.Memory namespace represents a memory pool. MemoryPool 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 vs. MemoryPool

The ArrayPool class rents out arrays using the Shared property, while the MemoryPool class rents out IMemoryOwner implementations. You should use ArrayPool if you need to create array instances repeatedly. And you should use MemoryPool if you’re working with Memory 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

How to do more in C#:

joydip_kanjilal
Contributor

Joydip Kanjilal is a Microsoft Most Valuable Professional (MVP) in ASP.NET, as well as a speaker and the author of several books and articles. He received the prestigious MVP award for 2007, 2008, 2009, 2010, 2011, and 2012.

He has more than 20 years of experience in IT, with more than 16 years in Microsoft .Net and related technologies. He has been selected as MSDN Featured Developer of the Fortnight (MSDN) and as Community Credit Winner several times.

He is the author of eight books and more than 500 articles. Many of his articles have been featured at Microsoft’s Official Site on ASP.Net.

He was a speaker at the Spark IT 2010 event and at the Dr. Dobb’s Conference 2014 in Bangalore. He has also worked as a judge for the Jolt Awards at Dr. Dobb's Journal. He is a regular speaker at the SSWUG Virtual Conference, which is held twice each year.

More from this author