7

I was reading the official ASP.NET Core Performance Best Practices documentation on Microsoft Website.

In order to increase performance, they recommend to use ArrayPool to store large array.

But, what is ArrayPool and how does that work ? Looking on internet and official documentation didn't help me to understand how it works and in which scenario I should use it.

Robouste
  • 3,020
  • 4
  • 33
  • 55
  • 3
    Allocating memory is expensive. If you have logic that does this frequently, you can use a pool to reuse memory. Previous array buffers are kept in memory and are reused to avoid new allocations. It works the same way as any other pool works. – Kieran Devlin Nov 15 '19 at 09:13
  • 2
    Please go through this https://adamsitnik.com/Array-Pool/ – Dipak Delvadiya Nov 15 '19 at 09:18

2 Answers2

8

Adding to the existing answer:

Remember, using ArrayPool instead of allocating with new puts the responsibility of freeing the memory on you. Your application will not leak memory if you don't guarantee that the Return method is called, but the ArrayPool is prevented from reusing the memory, thus denying the benefits you gain from using ArrayPool.

In simple use cases where you create a buffer and release it in the same method, it makes sense to put it into a finally clause:

private static void LocalUseOfSharedPool(int i)
{
    int[] arr = ArrayPool<int>.Shared.Rent(ARRAYSIZE);
    try
    {
        ShowAddress($"simple array {i}", arr);
        FillTheArray(arr);
        UseTheArray(arr);
    }
    finally
    {
        ArrayPool<int>.Shared.Return(arr);
    }
}

In more complex cases you must make sure to not leak the memory in other ways.

Also note, that your buffer now has a lifetime. If you pass your array to another object B , you need to make sure, that object B is not using the array after your call to ArrayPool<>.Return.

Since using ArrayPool is a performance issue, measure the gains of using it, especially if you want to change an existing system.

user1781290
  • 2,674
  • 22
  • 26
  • 2
    This is not entirely correct. Failing to Return an array is not a fatal error. The array will simply not be returned to the pool, and garbage collected like normal. On the other hand returning an array, and then still continuing to use it is a fatal error. – Christian Lundheim Dec 14 '21 at 18:47
  • "and garbage collected like normal", sorry I thought arraypool was using unmanaged memory, was there any clear indication that arraypool was using managed or unmanaged memory? – ck1521 Jan 27 '22 at 05:54
  • Oh nevermind I found it in the source and it is simply a new , it's weird that MemoryPool could go for unmanaged memory while ArrayPool can't... – ck1521 Jan 27 '22 at 06:07
  • @ChristianLundheim Thank you. I corrected that in the answer – user1781290 Feb 07 '22 at 10:56
1

We are looking to switch to arraypool because we allocate very large arrays in order to respond to http requests and we were running out of "free" memory in the process which was causing memory allocation delays upwards of 10+ seconds randomly.

For full details, see this post: Is correct to use GC.Collect(); GC.WaitForPendingFinalizers();?

Justin
  • 1,303
  • 15
  • 30