Sometimes we need to create and reuse a lot of large arrays of different types, which is inconvenient.
There is already a solution to this problem, like ArrayPool
, but it has drawbacks:
- It is required to return arrays manually to array pool, which is unsafe and can lead to bad memory utilization
- It is not flexible in underlying memory utilization in such a way, that arrays of different types cannot be reused independently.
So by RentedArray
you
RentedArray
isIDisposable
and will return used array in 100% cases even if you didn't disposed it manually(will do by garbage collector)RentedArray
always usesbyte
arrays to map any unmanaged data type you use, so evenRentedArray
of different types can reuse the same memory.
It's as simple as following:
void Foo()
{
using var array = ArrayPoolStorage.RentArray<int>(100000);
//....do stuff with array
}