PROWAREtech

articles » current » dot-net » memory-mapped-files

.NET: Memory-Mapped Files

Memory-mapped files allow sharing large amounts of data between processes; they are explored here with examples in C#.

Memory-mapped files provide a powerful method for inter-process communication (IPC) and large-scale data manipulation. They map the contents of a file directly into the virtual memory space of a process. This method is particularly efficient for accessing large files without loading the entire file into memory, and it's excellent for performance when multiple processes need to access the same data.

What Are Memory-Mapped Files

A memory-mapped file creates a virtual memory block which appears to the application as if it were part of the physical memory. Access to this virtual memory block translates directly to the contents of a file on disk. This approach allows applications to:

  • Read and write to the file by simply reading and writing to memory, using standard pointer operations or array indexing.
  • Share this memory between multiple processes, enabling IPC.

How Do Memory-Mapped Files Work?

1. Mapping a File to Memory:

  • When a file is mapped to memory, the operating system allocates a portion of the virtual address space of the process to be directly associated with the file.
  • The operating system manages the memory for mapped files efficiently by only loading segments (pages) of the file into physical memory as needed. This is done through paging.
  • Changes made to the memory are reflected in the file, and if the file is shared among several processes, changes made by one process are visible to others.

2. Virtual Memory and Paging:

  • The operating system uses virtual memory management techniques, such as paging, to manage the contents of memory-mapped files.
  • Only parts of the file that are being accessed are loaded into physical memory, while the rest remains on disk until needed.
  • If a process accesses an area of memory that corresponds to a part of the file not currently loaded into physical memory, a page fault occurs, and the operating system loads the needed segment automatically.

3. Performance Benefits:

  • Memory-mapping files is faster for random access and can be much more efficient than conventional file access methods (like read() and write()), especially when dealing with large data sets.
  • Since the operating system caches the file data in memory, subsequent accesses to the data are very fast.

Using Memory-Mapped Files in .NET

In .NET, work with memory-mapped files using the System.IO.MemoryMappedFiles namespace, which provides classes and methods to create and manage memory-mapped files.

Here’s a simple example of how to create and use a memory-mapped file in .NET:


using System;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Runtime.InteropServices;

class Program
{
	static void Main()
	{
		string mapName = "SampleMap";
		long capacity = 1024; // NOTE: size in bytes

		// NOTE: Create a memory-mapped file
		using (MemoryMappedFile mmf = MemoryMappedFile.CreateOrOpen(mapName, capacity))
		{
			// NOTE: Create a view accessor to interact with the memory-mapped file
			using (MemoryMappedViewAccessor accessor = mmf.CreateViewAccessor())
			{
				int value = 42;
				accessor.Write(0, value); // NOTE: Write an integer value at the beginning
				Console.WriteLine("Value written to memory-mapped file: " + value);

				// NOTE: Read the value back
				int readValue = accessor.ReadInt32(0);
				Console.WriteLine("Value read from memory-mapped file: " + readValue);
			}
		}
	}
}
Writing and Reading Strings

To write and read strings to/from a memory-mapped file:

  • Convert the string to a byte array using the chosen encoding.
  • Write the byte array to the memory-mapped file.
  • Optionally, store the length of the byte array if the string size varies.

using System;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Text;

class Program
{
	static void Main()
	{
		string mapName = "SampleMap";
		long capacity = 4096; // NOTE: Adjust size based on expected data volume

		// NOTE: Create a memory-mapped file
		using (MemoryMappedFile mmf = MemoryMappedFile.CreateOrOpen(mapName, capacity))
		{
			// NOTE: Create a view accessor
			using (MemoryMappedViewAccessor accessor = mmf.CreateViewAccessor())
			{
				string text = "This is a large string that needs to be written to a memory-mapped file.";
				byte[] bytes = Encoding.UTF8.GetBytes(text); // NOTE: Encoding the string as UTF-8

				accessor.WriteArray(0, bytes, 0, bytes.Length); // NOTE: Writing bytes to memory-mapped file
				Console.WriteLine("String written to memory-mapped file.");

				// NOTE: Reading back
				byte[] readBytes = new byte[bytes.Length];
				accessor.ReadArray(0, readBytes, 0, readBytes.Length);
				string readText = Encoding.UTF8.GetString(readBytes); // NOTE: Decoding the bytes back to a string

				Console.WriteLine("String read from memory-mapped file: " + readText);
			}
		}
	}
}

Determining the Size of a Memory-Mapped File

When working with memory-mapped files, knowing the size of the file being mapped or the size of the data to read is crucial for managing and navigating the data effectively. In many scenarios, the size of the memory-mapped file must be determined before creating or accessing the memory-mapped view, especially when needing to specify how much of the file to map. For memory-mapped files that do not directly map to existing disk files (system memory-backed), managing the size can be trickier because they are essentially of a dynamic size defined at creation time. Here’s how to handle this:

  • When creating the file, specify the maximum size it might need to be.
  • Store the actual data size within the file itself, either at the beginning of the mapped data or in a separate metadata store.

Example of setting the initial size and writing to it:


long initialSize = 1024 * 1024; // NOTE: For example, 1 MB
using (MemoryMappedFile mmf = MemoryMappedFile.CreateNew("MemoryMappedFileName", initialSize))
{
	using (MemoryMappedViewAccessor accessor = mmf.CreateViewAccessor())
	{
		byte[] data = Encoding.UTF8.GetBytes("Hello, this is a test.");
		accessor.Write(0, (int)data.Length);  // NOTE: First write the size
		accessor.WriteArray(sizeof(int), data, 0, data.Length);  // NOTE: Then write the data
	}
}

To read and use the dynamic size:


using (MemoryMappedFile mmf = MemoryMappedFile.OpenExisting("MemoryMappedFileName"))
{
	using (MemoryMappedViewAccessor accessor = mmf.CreateViewAccessor())
	{
		int dataSize = accessor.ReadInt32(0);  // NOTE: Read the stored size
		byte[] data = new byte[dataSize];
		accessor.ReadArray(sizeof(int), data, 0, dataSize);  // NOTE: Read the actual data
		Console.WriteLine(Encoding.UTF8.GetString(data));
	}
}

Use Cases for Memory-Mapped Files

  • Handling Large Files: They allow efficient handling of large data sets by letting the operating system handle the memory.
  • Shared Memory for IPC: Multiple processes can share the same memory-mapped files, allowing for fast and efficient IPC.
  • Data Persistence: Data can persist in a file, which can be a desirable trait for some applications after the process has exited.

Memory-mapped files offer a unique combination of high performance, data persistence, and the simplicity of accessing shared data, making them ideal for applications that need to manipulate large files or implement fast IPC mechanisms.


This site uses cookies. Cookies are simple text files stored on the user's computer. They are used for adding features and security to this site. Read the privacy policy.
CLOSE