Developer knowledge network · moderated exchange

Społeczność UnreliableCode

Badania programistów, inżynieria wsteczna i społeczność programistów

Knowledge indexNa żywo
4Categories
919Threads
2.8KPosty
Tutorial

High-Performance Zero-Copy Inter-Process Communication with MemoryMappedFiles in C# [StackOverflow Architecture Guide]

unsafe_memory_wizard
Unsafe & P/Invoke
MEMBER
Rozpustnik: 160
Data dołączenia: Jul 2022
Posty: 10
Dzięki: 38
1 miesięcy temu · Jul 8, 2026 9:41 PM
#1

Sharing memory buffers between separate C# processes with microsecond latency:

CSHARP
// Process A (Writer)
using var mmf = MemoryMappedFile.CreateOrOpen("Global\\MySharedData", 1024 * 1024);
using var accessor = mmf.CreateViewAccessor();
accessor.Write(0, 42);

// Process B (Reader)
using var mmfReader = MemoryMappedFile.OpenExisting("Global\\MySharedData");
using var readerAccessor = mmfReader.CreateViewAccessor();
int value = readerAccessor.ReadInt32(0);

Both processes read and write directly to the same physical RAM page frame backed by the OS virtual memory manager!

dotnet_runtime_architect
.NET Core Specialist
MEMBER
Rozpustnik: 103
Data dołączenia: Apr 2018
Posty: 40
Dzięki: 24
1 miesięcy temu · Jul 8, 2026 11:56 PM
#2

Memory mapped files provide the highest throughput possible for local IPC on Windows and Linux.

profiler_pat
Performance Hunter
MEMBER
Rozpustnik: 146
Data dołączenia: Aug 2019
Posty: 33
Dzięki: 31
1 miesięcy temu · Jul 9, 2026 10:17 AM
#3

You can also obtain a byte* pointer via accessor.SafeMemoryMappedViewHandle.AcquirePointer for direct Span<T> wrapping!