I'm looking for free, fully-managed implementations of shared memory for .NET (P/Invoke is acceptable, mixed C++/CLI is not).
-
and what about C++/CLI which compiles to MSIL, not mixed (e.g. compile with `/clr:pure`)? – Ben Voigt Jan 02 '11 at 21:58
-
1Sharing memory is easy enough, but you do realize that only primitive types can be shared? (and probably value types where all members are primitive) – Ben Voigt Jan 02 '11 at 22:02
-
Another valid option would be NamedPipes – Adrian Zanescu Aug 05 '15 at 11:54
-
[This article](http://www.codeproject.com/KB/threads/csthreadmsg.aspx) explains in section 3 how to wrap MapViewOfFile and friends using P/Invoke. – Martin v. Löwis Jan 02 '11 at 22:10
2 Answers
Well, the .NET framework is free, recommended. .NET 4.0 supports the System.IO.MemoryMappedFiles namespace classes. Shared memory is a fairly drastic impedance mismatch with the notion of a garbage collector, that's why it took a while. Unless you use pointers, copying from the GC heap to the MMF view is inevitable.
The other IPC mechanisms use shared memory too, it just isn't explicit since its built into the kernel. This all runs at roughly the same speed, a microsecond to setup the mapping and then just the bus bandwidth to do the memory-to-memory copy. Five gigabytes per second is the slowest you'll run into.

- 922,412
- 146
- 1,693
- 2,536
Sounds like you are looking for Memory-Mapped Files, which are supported in the .NET 4.0 BCL.
Starting with the .NET Framework version 4, you can use managed code to access memory-mapped files in the same way that native Windows functions access memory-mapped files, as described in Managing Memory-Mapped Files in Win32 in the MSDN Library

- 83,269
- 19
- 178
- 237
-
Looks like those and a shared mutex are gonna do the trick. After all I just need to pass some doubles with extremely low latency across two processes for some financial stuff. Thanks – em70 Jan 02 '11 at 23:05