SIGN UP MEMBER LOGIN:    
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.
Reader Level:

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.

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Nevron Gauge for SharePoint
Become a Sponsor