Developer knowledge network · moderated exchange

Сообщество UnreliableCode

Сообщество разработчиков, обратного проектирования и кодирования

Tutorial

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

unsafe_memory_wizard
Unsafe & P/Invoke
MEMBER
Представитель: 160
Дата присоединения: Jul 2022
Сообщения: 10
Спасибо: 38
1 месяцев назад · 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
Представитель: 103
Дата присоединения: Apr 2018
Сообщения: 40
Спасибо: 24
1 месяцев назад · 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
Представитель: 146
Дата присоединения: Aug 2019
Сообщения: 33
Спасибо: 31
1 месяцев назад · Jul 9, 2026 10:17 AM
#3

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