DEV Community

Cover image for C# - Efficient File I/O with Memory-Mapped Files
Keyur Ramoliya
Keyur Ramoliya

Posted on

C# - Efficient File I/O with Memory-Mapped Files

Memory-mapped files are a powerful feature in C# for efficiently reading and writing large files, especially when dealing with files that don't fit entirely in memory. They allow you to map a portion of a file directly into memory, significantly improving performance.

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

class Program
{
    static void Main()
    {
        // Define the file path and size
        string filePath = "largefile.txt";
        long fileSize = 1024 * 1024 * 100; // 100 MB

        // Create or open the memory-mapped file
        using (var mmf = MemoryMappedFile.CreateFromFile(filePath, FileMode.OpenOrCreate, null, fileSize))
        {
            // Create a memory-mapped view accessor to read and write data
            using (var accessor = mmf.CreateViewAccessor())
            {
                // Write data to the memory-mapped file
                string dataToWrite = "Hello, Memory-Mapped Files!";
                byte[] dataBytes = Encoding.UTF8.GetBytes(dataToWrite);
                accessor.WriteArray(0, dataBytes, 0, dataBytes.Length);

                // Read data from the memory-mapped file
                byte[] readData = new byte[dataBytes.Length];
                accessor.ReadArray(0, readData, 0, readData.Length);

                string readDataString = Encoding.UTF8.GetString(readData);
                Console.WriteLine("Data read from memory-mapped file: " + readDataString);
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

In this example:

  1. We define the file path (filePath) and size (fileSize) for the memory-mapped file. In this case, we're working with a 100 MB file.

  2. We create or open the memory-mapped file using MemoryMappedFile.CreateFromFile. If the file doesn't exist, it will be created.

  3. We create a memory-mapped view accessor using mmf.CreateViewAccessor(). This accessor allows us to read and write data to the memory-mapped file efficiently.

  4. We write data to the memory-mapped file using accessor.WriteArray. In this example, we write a string to the file after converting it to bytes.

  5. We read data from the memory-mapped file using accessor.ReadArray. We read the data back into a byte array and then convert it to a string.

Memory-mapped files are an excellent choice for large files because they provide efficient random access to data, reduce I/O overhead, and significantly improve read and write performance. They are particularly useful for scenarios like log file processing or working with large datasets that don't fit entirely in memory.

Top comments (0)