How Does a Memory-Mapped File Work?

Workstation motherboard with memory modules and a solid-state drive beside an open laptop

How Does a Memory-Mapped File Work? Explainer

A memory-mapped file is a file region that the operating system exposes as a range of virtual memory addresses. Instead of repeatedly asking read or write to copy bytes between a file and an application buffer, a program calls an interface such as mmap and then accesses the mapped addresses with ordinary loads and stores. The mapping does not place the entire file in physical RAM immediately. It creates a relationship among the process’s virtual address range, the file’s offsets, and the operating system’s page cache.

When the process first touches a mapped page that is not resident, the CPU raises a page fault. The kernel checks that the address and requested access are valid, finds the corresponding file offset, obtains the page from storage or the cache, updates the process’s page tables, and resumes the instruction. Later accesses can proceed like other memory references until a page is evicted or its permissions change. This demand-paging behavior lets a program work with a file larger than available RAM, although performance can collapse if the access pattern repeatedly pulls in more pages than memory can retain.

The mapping flags define how updates behave. With a shared mapping, writes can modify the cached file pages and eventually become visible in the underlying file according to the operating system’s writeback rules; synchronization calls can request a particular persistence point. With a private mapping, the process begins with the same file data, but modified pages are handled through copy-on-write and are not written back to the file. Protection flags separately control whether mapped pages may be read, written, or executed. File offsets must follow page-alignment rules, and unmapping removes the virtual-address relationship.

Memory mapping can simplify random access, sharing, and program loading. Two processes mapping the same file may use the same physical cached pages, reducing duplicate copies. Databases and search tools can address records by offset, while executable loaders map program segments and libraries into a process. Yet mapping does not remove storage latency or durability rules. A pointer access may unexpectedly trigger disk I/O, and errors such as accessing beyond a shortened file can become signals rather than ordinary read errors. Applications must also coordinate concurrent updates just as they would with any shared data.

Visibility and durability are separate questions. With a shared mapping, another process that maps the same region may observe changes through the common page cache, but synchronization is still needed to define when complex data structures are consistent. Dirty pages can remain only in memory until the kernel’s writeback policy or an explicit synchronization request sends them toward storage. Even then, an application that needs crash durability may have to coordinate file data, file size, metadata, and the guarantees of the filesystem and device. With a private mapping, a write creates a process-private page, so it cannot be used as a shortcut for updating the original file. These distinctions are why mapped access can look like ordinary memory while still requiring file-system reasoning.

The best choice depends on the workload. Memory mapping is attractive when software repeatedly accesses scattered parts of a file, shares read-only data, or benefits from letting the kernel manage caching. Explicit I/O may be clearer when the program needs precise error handling, predictable prefetching, direct-I/O behavior, or strict control over when data is copied and persisted. Large address spaces make mapping convenient, but 32-bit processes face tighter limits. A memory-mapped file should therefore be understood as a virtual-memory view of stored data: efficient and expressive in the right pattern, but still governed by page faults, caching, synchronization, permissions, and the reliability of the underlying file.

A memory-mapped file links a file region to virtual addresses so a process can access file data with normal memory operations. Key terms include mmap, virtual address, page, page fault, page cache, MAP_SHARED, MAP_PRIVATE, copy-on-write, protection, msync, and munmap.

If the page is not resident, the CPU faults into the kernel. The kernel validates the mapping, finds or reads the file page, installs a page-table entry, and restarts the instruction. Later accesses use the mapped page until it is evicted or invalidated.

Mapping may be awkward when software needs explicit per-operation errors, exact I/O scheduling, direct access that bypasses the page cache, or simple handling of files that can shrink concurrently. Poor locality can also cause repeated page faults and severe performance loss.

Explore more "Explainers"

Discover additional explainers across politics, science, business, technology, and other fields. Each explainer breaks down a complex idea into clear, everyday language—helping you better understand how major concepts, systems, and debates shape the world around us.