Reader Level:
ARTICLE

Using Memory-Mapped Files

Posted by Vulpes Articles | .NET 4.5 March 30, 2011
MemoryMappedFile is an interesting new class in version 4.0 of the .NET Framework which resides in the System.IO.MemoryMappedFiles namespace.
  • 0
  • 0
  • 22042

Introduction

MemoryMappedFile is an interesting new class in version 4.0 of the .NET Framework which resides in the System.IO.MemoryMappedFiles namespace.

A memory-mapped file is a feature of the Windows operating system which allows memory to be shared between two or more processes running on the same machine. It requires much less overhead than other methods of inter- process communication such as remoting or WCF.  

It also allows fast random access to that memory - much faster than file streams or pipes which are optimized for sequential access.

Whilst it was possible in earlier versions of .NET to create and access memory-mapped files by calling Windows API functions, this was rather a tortuous process and the new class wraps the native functions in an easy to use and intuitive way.

Example of use

Suppose we want to create a 1,000 byte file and share it with another process. The following program (program1.cs) does this:

using System;
using System.IO.MemoryMappedFiles;

class Program1
{
    static void Main()
    {
        // create a memory-mapped file of length 1000 bytes and give it a 'map name' of 'test'
        MemoryMappedFile mmf = MemoryMappedFile.CreateNew("test", 1000);
        // write an integer value of 42 to this file at position 500
        MemoryMappedViewAccessor accessor = mmf.CreateViewAccessor();
        accessor.Write(500, 42);
        Console.WriteLine("Memory-mapped file created!");
        Console.ReadLine(); // pause till enter key is pressed
        // dispose of the memory-mapped file object and its accessor
        accessor.Dispose();
        mmf.Dispose();
    }
}   

When we run this program, we need to refrain from pressing the 'enter' key until the following program (program2.cs), running on the same machine, has read the integer we've written to the memory-mapped file and printed it to the console:

using System;
using System.IO.MemoryMappedFiles;
class Program2
{
    static void Main()
    {
        // open the memory-mapped with a 'map name' of 'test'
        MemoryMappedFile mmf = MemoryMappedFile.OpenExisting("test");
        // read the integer value at position 500
        MemoryMappedViewAccessor accessor = mmf.CreateViewAccessor();
        int value = accessor.ReadInt32(500);
        // print it to the console
        Console.WriteLine("The answer is {0}", value);
        // dispose of the memory-mapped file object and its accessor
        accessor.Dispose();
        mmf.Dispose();
    }
}
  
The output should, of course, be:

      The answer is 42

Other points to note

Memory-mapped files don't have to be shared with other processes but can simply be used where you need fast random access to an existing (or newly created) disk file. The method to create the file is then MemoryMappedFile.CreateFromFile and the 'map name' can be null.

View accessors can only read or write what's known as 'unmanaged types' bearing in mind that they need to be mapped into unmanaged memory. These are the simple value types (int, bool, double, long ,decimal etc.) or other structs which don't contain any reference type fields. This means that strings, which are reference types, have to be dealt with as arrays of bytes.

Memory-mapped files also allow us to work with a very large disk file without having to load all of it into working memory at once because you can create a view of a part of the file rather than the whole of the file.

COMMENT USING