C# ArrayPool — Complete Guide
In this tutorial, you'll learn about C# ArrayPool. We cover key concepts, practical examples, and best practices.
You need a temporary byte array for serialization, but allocating a new one each time creates GC pressure. ArrayPool<T> provides a pool of reusable arrays — rent one, use it, return it. No allocations after warmup.
Wrong
public byte[] Serialize(Message msg)
{
using var ms = new MemoryStream();
JsonSerializer.Serialize(ms, msg);
return ms.ToArray();
}
Output: Works. Multiple allocations: MemoryStream internal buffer grows, ToArray() allocates the final array.
Right
public byte[] Serialize(Message msg)
{
using var ms = new MemoryStream();
JsonSerializer.Serialize(ms, msg);
byte[] result = ms.GetBuffer();
int length = (int)ms.Length;
// Rent a buffer and copy
byte[] pooled = ArrayPool<byte>.Shared.Rent(length);
Buffer.BlockCopy(result, 0, pooled, 0, length);
// Use pooled array...
ArrayPool<byte>.Shared.Return(pooled);
return result;
}
More practical — rent for temporary work:
byte[] buffer = ArrayPool<byte>.Shared.Rent(4096);
try
{
int bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length);
ProcessBuffer(buffer, bytesRead);
}
finally
{
ArrayPool<byte>.Shared.Return(buffer);
}
Prevention
- Use
ArrayPool<T>.Shared.Rent(minLength)to get a reusable array. - Use
try-finallyorusing(via a wrapper) to guarantee return. - Use
ArrayPool<T>.Shared.Return(array, clearArray: true)to clear sensitive data. - Rent for temporary buffers in hot paths (serialization, I/O, crypto).
- Do not hold rented arrays longer than necessary — return them promptly.
- Use
MemoryPool<T>when you needMemory<T>for async operations. - Never use a returned array — it may be loaned to another consumer.
Common Mistakes with array pool
- Using
foldlinstead offoldl'causing stack overflow on large lists - Forgetting
deriving (Show, Eq)on custom data types needed for debugging - Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
These mistakes appear frequently in real-world CSHARP code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
ArrayPool<byte> is used in DodaZIP for zero-allocation compression buffers. For more C# performance, visit DodaTech.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro