C# Allocate Fast Unmanaged Memory or Large Blocks, VirtualAlloc, AllocHGlobal

 

2 thoughts on “C# Allocate Fast Unmanaged Memory or Large Blocks, VirtualAlloc, AllocHGlobal”

  1. While stumbling around the net looking for better ideas to several situations I came across this nifty snippet. I have a need for allocating unmanaged memory as a device I am using needs a fixed buffer to drop video frames in. Today that is being done by allocating a Byte[] on the managed side and the allocating a GCHANDLE of type Pinned. Essentially, pinning managed memory for the duration. Not sure that is the best option. I like your solution as it allocates memory from outside the application space. I would like to use this but the buffers have to be on 4K boundaries. How would one do that with your Alloc?

    Thanks,
    Doug

    1. Douglas,

      Both AllocHGlobal and VirtualAlloc use the default page boundary of 4K. Although the minimum granularity of both is 64K. While both of the functions are outside of the GC, there will be wasted memory when allocating small blocks, because of the 64k minimum granularity.

Leave a Reply

Your email address will not be published. Required fields are marked *