2

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.

Ryan Peschel
  • 11,087
  • 19
  • 74
  • 136
  • 1
    Read this http://stackoverflow.com/questions/2648560/allocating-unmanaged-memory-in-c-sharp – Sorceri Mar 13 '15 at 14:44
  • @Ryan Yes, you can allocate memory in various ways, no, you can't make the CLR use the memory you allocated as/for a .NET Array/List/??? – xanatos Mar 13 '15 at 14:45
  • You can always create a gigantic array and logically subdivide it into smaller arrays.. –  Mar 13 '15 at 14:47
  • Expounding on Will's statement, this can be done simply by creating a byte[1000] and then having a struct containing the position, and size of the smaller array. Which can then be stored in a List. – Enfyve Mar 13 '15 at 14:51

1 Answers1

4

It is hard to answer without more context. But it sounds like you're looking for something like ArraySegment which is an abstraction to access the portion of array.

With ArraySegment you can just create a single large array and split it with multiple segments logically.

If the millions of arrays you create are short lived, then it is fine to create and forget them. Garbage collector is good at handling short lived references.

Remember time taken to garbage collection is not directly proportional to the number of objects you create, but proportional to the number of objects which survive the garbage collection. So, if your objects are short lived, you're better off creating many arrays.

Creating a huge array is a bad thing to do if it is more than 85,000 bytes, because it will sit in LOH which is not compacted on GC. No compaction -> Large chances of fragmentation.

Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189