C#, byte arrays, avoid memory fragmentation

642 Views Asked by At

I'm writing a data structure, which main purpose will be containing binary data (byte arrays) organized in such way, that user can insert data in the middle without performance penalties (moving large chunks of memory).

For that purpose, I'll need to create a lot of smaller byte-arrays, which will serve as buffers for parts of the bigger structure. The problem is that I might need to juggle those small arrays frequently and I'm worried about possible memory fragmentation.

My idea to avoid it is to ensure, that all arrays I use within this structure will have one of two sizes, say 1024 or 2048 bytes (or similar, generally n and 2*n). I hope to simplify work of memory manager, which will be able to reuse memory after disposed arrays.

Will it work that way? Or should I design some kind of array pool and implement the array-reusing mechanism myself?

0

There are 0 best solutions below