Working with memory-mapped files in C#

Posted: (EET/GMT+2)

 

Most .NET code works with files using streams: FileStream, StreamReader and so on. But if you need to access large files efficiently, or you want to share memory between processes, memory-mapped files are worth a look.

The MemoryMappedFile class in the System.IO.MemoryMappedFiles namespace lets you map a file (or part of it) directly into memory. After that, you can access it through a MemoryMappedViewAccessor or MemoryMappedViewStream.

Here is a minimal example:

using System.IO.MemoryMappedFiles;

using (var mmf = MemoryMappedFile.CreateFromFile(
    "data.bin", FileMode.OpenOrCreate, "MyMap", capacity: 1024))
{
    using (var accessor = mmf.CreateViewAccessor())
    {
        accessor.Write(0, 42); // write an int at offset 0
        int value = accessor.ReadInt32(0);
    }
}

Because the file is mapped, the operating system's virtual memory system does the heavy lifting. You do not need to read the entire file into RAM at once. This can make a big difference when working with multi-gigabyte data.

Another powerful use case is inter-process communication: two processes can open the same named memory-mapped file and share data by writing into the mapped region. Just remember to coordinate access carefully if both sides are writing.

You won't need memory-mapped files every day, but they're good to have in your C# toolbox for cases where streams start to feel too heavy or too limiting.