.Net Framework 4.0: Using memory mapped files

.Net Framework 4.0 introduces memory mapped files. Memory mapped files are useful when you need to do in-memory data manipulation and your data structures are large. For large in-memory data the performance of memory mapped file is the best. It is much faster thanMemoryStream. And like files on hard disc, memory mapped files can be shared between different programs. MemoryMappedFile and other classes for memory mapped files can be found from System.IO.MemoryMappedFiles namespace.

Now let’s see the example that contains two applications: one of them is primitive server that creates and holds memory mapped file and the other is simple client that reads that file. In the end of the posting you can find download link for example Visual Studio 2010 solution.

MemoryMappedFile server

Our server, as I said before, is primitive. It creates memory mapped file and writes some bytes to our file. Then it starts waiting key press from user to release resources and exit. The code of server is here.


static void Main(string[] args)
{
   
Console.WriteLine("Memory mapped file server started"
);

   
using (var file = MemoryMappedFile.CreateNew("myFile", int
.MaxValue))
    {
       
var bytes = new byte
[24];
       
for (var
i = 0; i < bytes.Length; i++)
            bytes[i] = (
byte
)(65 + i);

       
using (var
writer = file.CreateViewAccessor(0, bytes.Length))
        {
            writer.WriteArray<
byte
>(0, bytes, 0, bytes.Length);
        }
       
Console.WriteLine("Run memory mapped file reader before exit"
);
       
Console.WriteLine("Press any key to exit ..."
);
       
Console.ReadLine();
    }
}

MemoryMappedFile reader

The client project opens memory mapped file and reads bytes that server wrote there. I hope you notice that I hard coded the count of bytes that reader reads from memory mapped file. The point is simple – we are accessing memory mapped file directly without any contracts for file structure. In reality there will be some API that you use to manager memory mapped file in your application.


static void Main(string[] args)
{
   
Console.WriteLine("Memory mapped file reader started"
);

   
using (var file = MemoryMappedFile.OpenExisting("myFile"
))
    {
       
using (var
reader = file.CreateViewAccessor(0, 24))
        {
           
var bytes = new byte
[24];
            reader.ReadArray<
byte
>(0, bytes, 0, bytes.Length);

           
Console.WriteLine("Reading bytes"
);
           
for (var
i = 0; i < bytes.Length; i++)
               
Console.Write((char)bytes[i] + " "
);

           
Console.WriteLine(string
.Empty);
        }
    }

   
Console.WriteLine("Press any key to exit ..."
);
   
Console.ReadLine();
}

Now run server (MemoryMappedFileCreate) and after that the client (MemoryMappedFileRead). Server creates new memory mapped file and writes there 24 bytes of characters. Client then reads these bytes and writes them to console.

To find out more about memory mapped files feel free to read MSDN Utopia blog entryWorking with memory mapped files in .NET 4.


One thought on “.Net Framework 4.0: Using memory mapped files

  • Me says:

    It would be interesting to know how this works internally.
    E.g. how the int.MaxValue affects this?
    Does it allocate this amount of memory beforehand?
    What happens if you try to write more than that?
    Can yo rewind the file and overwrite old data to avoid it grow over time?

Leave a Reply

Your email address will not be published. Required fields are marked *