ArrayPool<T> for renting buffers
Context: ArrayPool<T> is a high‑performance pool of arrays that reduces garbage collection pressure by reusing large arrays. Instead of allocating a new array each time, you rent one from the pool and return it when done. This is especially useful for temporary buffers that are used frequently (e.g., in network or file I/O). The default ArrayPool<T> is shared and thread‑safe.
Usage Example
Section titled “Usage Example”using System;using System.Buffers;
public class ArrayPoolDemo{ public static void ProcessData(int minLength) { byte[] buffer = ArrayPool<byte>.Shared.Rent(minLength); try { // Use the buffer (actual length may be larger than minLength) // Fill buffer with data Console.WriteLine($"Rented buffer size: {buffer.Length}"); } finally { ArrayPool<byte>.Shared.Return(buffer); } }}Output console
Section titled “Output console”dotnet runRented buffer size: 1024Important notes
Section titled “Important notes”- Rented arrays may be larger than requested – only use the first
minLengthelements. - Always return the buffer to the pool to avoid memory leaks.
- Do not keep references to returned buffers.
Real‑world usage example
Section titled “Real‑world usage example”High‑throughput TCP server – When reading from sockets, renting a buffer from ArrayPool<byte> for each receive operation avoids frequent allocations, reducing GC pressure.
See .NET docs on ArrayPool