November 22, 2014
Hot Topics:

Using Memory-Mapped Files in .NET 4.0

  • July 14, 2009
  • By Jani Järvinen
  • Send Email »
  • More Articles »

Reading from the Mapped File

To use a previously mapped memory location, you need to use the methods of the MemoryMappedViewAccessor class. For instance, to read ten bytes starting from the beginning of the file map, you could use the ReadByte method as follows:

  ...
  MemoryMappedViewAccessor accessor =
    mmf.CreateViewAccessor();
  byte[] buffer = new byte[10];
  for (int index = 0; index < buffer.Length; index++)
  {
    buffer[index] = accessor.ReadByte(index);
  }

The Read method can either fill in the contents of a given general object, or it can take a more specific object using generics with Read<T> or ReadArray<T>. For instance, assume you had an object of type Guid (defined as a structure), then the two ReadNNN method calls below would have similar results:

  // method 1:
  byte[] buffer = new byte[16];
  accessor.ReadArray(0, buffer, 0, buffer.Length);
  Guid guid = new Guid(buffer);
  MessageBox.Show(guid.ToString());
  
  // method 2:
  Guid guid2 = new Guid();
  accessor.Read(0, out guid2);
  MessageBox.Show(guid2.ToString());

Note that in both Read method calls, you have to specify the location from which the reading is to begin. This zero- based offset is always relative to the mapped view, but not necessarily the original file. When you create the memory mapping object, you need to specify a window of memory through which you want to manipulate the file (Figure 1). If you don't specify any offset as in the code listings above, then the view is assumed to start from the beginning of the file.



Click here for larger image

Figure 1. View offsets are always relative to the mapped view.

To help providing flexibility, you can start from the offset of zero and run until the length of the file or you can start from the middle and map only a portion of the file. Reading through the accessor object is done by offsets relative to the view. That is, the original file offset would then be the view's starting offset plus the view offset.

Remember also that memory mapping objects and the files underneath have operating system handles. Thus, it is important to remember to dispose the objects after you are done with them; otherwise they will remain open for an indefinite amount of time until garbage collection kicks in. A good practice is to use try-finally blocks or use the C# using statements.

If you are happy working with .NET stream objects but would still like to benefit from memory-mapped files, then you are in luck. The MemoryMappedFile class contains a handy method called CreateViewStream, which returns a MemoryMappedViewStream object. This object allows sequential access to the mapped view; this is probably the biggest disadvantage of using mapped view streams compared to using the accessor objects which allow random access. But if you are happy with this limitation, then the CreateViewStream method is your friend.





Page 2 of 4



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel