Is it possible to avoid copying data when using memory mapped files in C #?

My understanding of how memory mapped files work in C # is that every data request leads to copying. For example, if you had a large data structure that was saved as a file, using a memory mapped file would result in memory for the actual file mapped to RAM and a copy located in the GC heap after reading it from the file.

I guess this is because pointers and GC don't get along well with each other at all.

So is there a way around this?

  • Perhaps through some C ++ mixed mode that can display a managed API through memory mapped data?
  • How about manipulating a direct pointer with unsafe C #?

The common problem I'm trying to solve is sharing a large data structure between multiple processes. The data structure is used to answer a small set of “questions” that can be represented as a simple API (i.e., basically a highly specialized index of many other data).

On the other hand, does this make the .NET API useless for the “data-sharing” scenario?

+5
source share
1 answer

You can use unsafe code to directly access the displayed memory. I suggest you take a look at “blittable structs”, which are types of structures that can be copied to memory without modification. Here is an example:

struct MyDataRecord { public int X, Y; }

...

for (var i = 0 .. 10) {
 ((MyDataRecord*)pointerToUnmanagedMemory)[i] = new MyDataRecord() { X = i, Y = i * i };
}

It is very convenient and convenient.

+4

All Articles