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()
- {
-
- MemoryMappedFile mmf = MemoryMappedFile.CreateNew("test", 1000);
-
- MemoryMappedViewAccessor accessor = mmf.CreateViewAccessor();
- accessor.Write(500, 42);
- Console.WriteLine("Memory-mapped file created!");
- Console.ReadLine();
-
- 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()
- {
-
- MemoryMappedFile mmf = MemoryMappedFile.OpenExisting("test");
-
- MemoryMappedViewAccessor accessor = mmf.CreateViewAccessor();
- int value = accessor.ReadInt32(500);
-
- Console.WriteLine("The answer is {0}", value);
-
- 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.