Skip to main content
InterviewHubPRO
Section Overview
Q19

Question 19 of 50

Part 2: Concurrency, Zero-Allocation & Advanced Language Features (11 - 24)

38%
C# & CLR Internals (50 Qs)•Part 2: Concurrency, Zero-Allocation & Advanced Language Features (11 - 24)

What is ArrayPool<T> and how does memory pooling eliminate Garbage Collection Gen 0 pressure?

senior LevelCode ExampleMemory ManagementData Structures

In Simple Words: Allocating temporary byte or char arrays in high-throughput services (e.g., HTTP request buffers) floods Gen 0 with short-lived objects, triggering frequent GC pauses. ArrayPool<T>.Shared rents pre-allocated arrays from a shared memory pool and returns them after use, reducing allocations to zero.

csharp
byte[] buffer = ArrayPool<byte>.Shared.Rent(4096);
try {
    int bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length);
    ProcessBytes(buffer.AsSpan(0, bytesRead));
} finally {
    ArrayPool<byte>.Shared.Return(buffer, clearArray: false);
}