In my program I am allocating a lot of arrays (millions) and they are all quite small in size.
Because it is faster to allocate a large block of memory than small ones repeatedly, I was looking for a way to just allocate a large block of memory, keep a pointer to the current position in that block to draw the next array from, and then allocating another large block when that one is exhausted.
Is this possible to do in C# and if so, how? I have a feeling Marshal.AllocHGlobal
may be what I'm looking for, but I'm not quite sure if C# has a more managed way of doing so.
As an example, I'd like to allocate a memory block of 1,000 bytes, and then later create an array of length 4 that uses the first 4 bytes, and then later create an array of length 3 that uses the next 3 bytes.