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.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay