I have a quite large float array (usually >40 million entries) which takes up between 150MB und 250MB megabytes in memory and I need to pass it to two different APIs. Both are thrid party tools which I can't change. One of which only accepts byte[] and the other one only ref float[].
It is not a problem for me to convert it to a byte[] using Buffer.BlockCopy or similar approaches. But due to memory fragmentation allocating an array of this size fails regularly, and I would like to avoid copying it if possible.
Is there any way, to accomplish this? Maybe using unsafe code or C++ cli or C++ or a combination of all of them? Or maybe using MemoryMappedFile? I have already tried all of these but so far without any success.
If I understood your question correctly, something like this:
GCHandle handle = GCHandle.Alloc(myObject, GCHandleType.Pinned);
try
{
IntPtr myPinnedPointer = handle.AddrOfPinnedObject();
// use myPinnedPointer for your needs
}
finally
{
handle.Free();
}
This question was previously tagged C++/CLI. In that context, byte[]
is somewhat ambiguous, you could be referring to a unmanaged C-style array (which uses the []
syntax in C++/CLI), or you could be referring to a managed array (which uses the []
syntax in C#, but not in C++/CLI.)
Since the question is now tagged only C#, I'm assuming that when you say []
, you're referring to managed arrays.
If the two APIs you need to use are byte[]
and float[]
in C#, there's no way to make them share the memory. If one of the APIs took something lower-level, such as a byte*
, there would probably be a way, but an array in C# is a full-fledged object, and the only thing it wants to do is allocate its own memory.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.