AlgoMaster Logo

mmap and Memory-Mapped Files

High Priority31 min readUpdated August 14, 2026

Suppose a service needs to search a 2 GiB index file. It could repeatedly call read() and copy chunks into user-space buffers, or it could map the file directly into its virtual address space.

Once mapped, the application can access the file using ordinary memory operations:

The pointer is still a virtual address. When the process first accesses a part of the file that is not in memory, a page fault lets the kernel bring in the corresponding page. Later accesses proceed like normal memory reads while the page remains resident.

The Unix interface for this is **mmap()**, short for memory map.

mmap() creates a virtual memory region backed by a file or anonymous memory.

Memory mapping connects virtual memory with file I/O. It does not load the entire file upfront, bypass the OS page cache, or guarantee that writes have already reached persistent storage.

This chapter explains file-backed and anonymous mappings, protection and sharing modes, alignment and lifetime rules, visibility and durability, page-cache behavior, failure cases, and the trade-offs between mmap and read.

Premium Content

Subscribe to unlock full access to this content and more premium articles.