My understanding of how memory mapped files work in C# is that every request for data results in a copy. For instance, if you had a large data structure persisted as a file, using a memory mapped file would result in memory for the actual file mapped into RAM, and a copy residing in a GC heap once it was read from the file.
I'm assuming this is because pointers and the GC don't get along well together generally speaking.
So, is there any way around this?
The general 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 exposed as a simple API (i.e. basically, a highly specialized index of a bunch of other data).
On a side note, doesn't this make the .NET API useless for the "sharing large amounts of data" scenario?
You can use unsafe code to directly access the mapped memory. I suggest you look into "blittable structs" which are struct types which can be copied around in 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 };
}
This is very performant and kind of convenient.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With