Using Memory-Mapped Files

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:
  1. using System;  
  2. using System.IO.MemoryMappedFiles;  
  3.    
  4. class Program1  
  5. {  
  6.     static void Main()  
  7.     {  
  8.         // create a memory-mapped file of length 1000 bytes and give it a 'map name' of 'test'  
  9.         MemoryMappedFile mmf = MemoryMappedFile.CreateNew("test", 1000);  
  10.         // write an integer value of 42 to this file at position 500  
  11.         MemoryMappedViewAccessor accessor = mmf.CreateViewAccessor();  
  12.         accessor.Write(500, 42);  
  13.         Console.WriteLine("Memory-mapped file created!");  
  14.         Console.ReadLine(); // pause till enter key is pressed  
  15.         // dispose of the memory-mapped file object and its accessor  
  16.         accessor.Dispose();  
  17.         mmf.Dispose();  
  18.     }  
  19. }    
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:
  1. using System;  
  2. using System.IO.MemoryMappedFiles;  
  3. class Program2  
  4. {  
  5.     static void Main()  
  6.     {  
  7.         // open the memory-mapped with a 'map name' of 'test'  
  8.         MemoryMappedFile mmf = MemoryMappedFile.OpenExisting("test");  
  9.         // read the integer value at position 500  
  10.         MemoryMappedViewAccessor accessor = mmf.CreateViewAccessor();  
  11.         int value = accessor.ReadInt32(500);  
  12.         // print it to the console  
  13.         Console.WriteLine("The answer is {0}", value);  
  14.         // dispose of the memory-mapped file object and its accessor  
  15.         accessor.Dispose();  
  16.         mmf.Dispose();  
  17.     }  

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.