Fact-checked by Grok 2 weeks ago

Memory-mapped file

A memory-mapped file is a mechanism in operating systems that associates the contents of a file or portion thereof with a segment of a process's , enabling the file data to be accessed directly as if it were resident in memory. This mapping is achieved through system calls or APIs, such as mmap() in systems, which create a region backed by the file on disk, allowing read and write operations via standard memory pointers without explicit I/O system calls. In practice, the operating system handles paging between disk and physical memory transparently, loading only the accessed portions into on demand. Memory-mapped files offer significant advantages for handling large datasets, as they avoid the overhead of loading entire files into memory or managing buffers manually, making them ideal for applications like , image processing, and . They support both shared and private mappings: shared mappings (MAP_SHARED in ) allow multiple processes to access and modify the same file region synchronously, with changes persisted to disk, while private mappings (MAP_PRIVATE) provide semantics for isolated modifications without affecting the underlying file. This duality enables efficient (IPC) and data sharing, as seen in scenarios where processes collaborate on common resources like libraries or files. The concept is natively supported across major operating systems, including Windows via the Win32 API functions like CreateFileMapping and MapViewOfFile, and Unix variants through POSIX-compliant mmap. Benefits include reduced I/O latency, simplified programming by treating files as arrays, and optimized resource usage for files exceeding available RAM, though limitations such as page alignment requirements and potential synchronization issues in multi-process environments must be managed. Overall, memory-mapped files represent a foundational technique for bridging persistent storage and volatile memory in modern software systems.

Fundamentals

Definition and Core Concept

A memory-mapped file is a that associates a portion or the entirety of a file's contents with a segment of a process's address space, enabling the operating system to manage file accesses transparently as if the data were resident in physical memory. This approach leverages the operating system's subsystem to treat disk-based files as an extension of , facilitating seamless integration between persistent storage and application memory. At its core, the concept involves using system calls to establish a direct correspondence between offsets and virtual addresses, allowing programs to perform reads and writes via standard operations rather than dedicated I/O functions like read() or write(). For instance, modifications to the mapped region are automatically propagated back to the underlying (in shared mappings), or buffered for later , depending on the mapping flags specified. This simplifies handling by eliminating the need for explicit management and I/O in many scenarios. Memory-mapped files depend on foundational features, such as paging, where memory is organized into fixed-size pages (typically 4 KB). Access to an unmapped or unloaded page triggers a exception, which the operating system's handles by fetching the relevant data from the file into a physical page frame and updating the to reflect the mapping. This demand-paging mechanism ensures that only actively accessed portions of the file consume physical memory, optimizing resource usage for large or sparsely accessed files. A basic example of setting up a memory mapping in C, using the mmap() function, illustrates this process:
c
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int fd = open("example.txt", O_RDWR);  // Open [file descriptor](/page/File_descriptor)
struct stat sb;
fstat(fd, &sb);
void *addr = mmap(NULL, sb.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
// Now 'addr' points to the mapped file content; access via pointers
// Unmap when done: munmap(addr, sb.st_size);
This code opens a , determines its size, and maps the entire into the process's starting at an OS-chosen address (), with shared permissions allowing changes to persist to disk.

Mapping Mechanism

To map a into a process's , an application first opens the using a such as open() in POSIX-compliant systems, obtaining a that references the on disk. The mapping is then established by invoking the , which takes parameters including the desired starting address (often specified as 0 to let the choose), the length of the mapping in bytes, protection flags (e.g., PROT_READ for read access or PROT_READ | PROT_WRITE for read-write access), mapping flags (such as MAP_SHARED or MAP_PRIVATE), the , and an offset into the where the mapping begins. These parameters define the portion of the to map and the behavior of accesses to that region, ensuring alignment with page boundaries (typically 4 KB) for efficient handling. Upon invocation of mmap(), the operating system creates a new region in the process's , associating it with the specified file segment without immediately loading the data into physical . This region is backed by the file on disk, and the updates the process's page tables to mark the virtual pages as valid but not yet resident in . When the process first accesses a byte within the mapped region—such as reading or writing—it triggers a because the corresponding physical page is absent. The 's page fault handler then intervenes via demand paging: it allocates a physical page if needed, reads the relevant file data from disk into the 's , and maps that page into the process's , resolving the fault and allowing the access to proceed transparently. Subsequent accesses to the same page hit the or physical , avoiding further disk I/O until the page is evicted under pressure. The flags control how modifications interact with the underlying file and other processes. With MAP_SHARED, changes made by to the mapped memory are propagated back to the file on disk and visible to other processes sharing the same , enabling or persistent updates. In contrast, MAP_PRIVATE uses a mechanism: initial reads reflect the file's content, but any write operation creates a private copy of the page for , isolating modifications from the file and other sharers to prevent unintended side effects. Protection flags enforce access controls at the hardware level through entries, raising segmentation faults for violations like writing to a read-only . The mmap() call can fail under various conditions, returning a (or (void *)-1 in ) and setting an via errno. Common failures include ENOMEM when the system lacks sufficient or physical to establish the , EINVAL for invalid parameters such as a non-page-aligned or exceeding the , or ENODEV if the does not support mapping (e.g., certain special files). Applications must check the return value to handle these errors gracefully, often falling back to traditional file I/O methods.

Historical Development

Early Systems and PMAP

The operating system, developed by (DEC) in the for the mainframe, introduced memory-mapped file capabilities through its PMAP monitor call, enabling efficient access to file contents by integrating them directly into a process's . Released in early 1976 as part of the initial distribution, this feature built on prior TENEX innovations and addressed the demands of environments where physical memory was limited to hundreds of kilobytes. The PMAP call (JSYS #56) allowed users to map one or more complete pages from a disk file into a process's memory for input, from a process to a file for output, or between processes, without the overhead of explicit data copying. Key features of PMAP included dynamic mapping of files as either executable code or data, supporting read, write, or execute access modes based on file attributes established via the OPENF call. In timesharing systems like TOPS-20, which supported dozens of concurrent users on a single PDP-10, PMAP facilitated efficient program loading by mapping executable or save files (such as SSAVE or SAVE formats) directly into virgin processes, bypassing traditional read protections for execute-only files when combined with the GET monitor call. This approach enabled random access to file pages beyond EOF limits and supported page-mode I/O, where pages could be preloaded into physical memory or linked with copy-on-write semantics to minimize resource use. Unmapping or deleting pages was also handled via PMAP, ensuring processes could release resources cleanly before file closure. PMAP demonstrated significant benefits for accessing large files in the resource-constrained era of mainframes, where swapping entire programs into limited was inefficient for multi-user workloads. By treating files as extensions of , it reduced I/O latency and memory fragmentation in scenarios, influencing later systems' approaches to management. For instance, in environments with up to 384K words of physical memory divided into 512-word pages, PMAP's ability to map specific page ranges or entire files streamlined and process communication without redundant copies.

Unix Implementations

SunOS 4, released by in December 1988, marked the first widespread implementation of the mmap() in a Unix operating system, enabling programs to map files directly into their for efficient access. This feature was part of a comprehensive overhaul, as described in the seminal 1987 paper "Virtual Memory Architecture in SunOS" by Robert A. Gingell, , and William A. Shannon, which outlined the segment-based mapping mechanism using drivers like seg_vn for file-backed segments. The mmap() call supported both shared and private () mappings at page granularity, allowing seamless integration of file I/O with and reducing the need for explicit read/write operations. The adoption of mmap() in Unix-like systems drew significant influence from the Berkeley Software Distribution (BSD), where it was first documented in 4.2BSD (1983) but fully implemented in 4.4BSD (1993), providing a foundation for memory-mapped I/O in academic and research environments. 4 itself was derived from BSD, incorporating these concepts into a production-ready system. Concurrently, mmap() was integrated into Release 4 (SVR4), released in 1988 by , which extended the interface to support mapping of general objects into address spaces via new system calls like mmap(2) and munmap(2), as detailed in the process model enhancements for /proc. This convergence between BSD and System V lineages facilitated broader compatibility. Standardization efforts culminated in the inclusion of mmap() in the specification, defining a portable for mapping files, objects, or typed memory into a process's across Unix variants, with flags for shared (MAP_SHARED) and private (MAP_PRIVATE) behaviors. ensured interoperability by specifying error conditions, protection modes (e.g., PROT_READ, PROT_WRITE), and advice parameters (e.g., MAP_SHARED for ), building on Unix implementations to promote application portability. A key benefit of mmap() in these Unix systems was its support for I/O, where file is accessed directly via memory references without user-kernel copies or additional calls for reads/writes, thereby minimizing switches and overhead in data-intensive applications. This extended to handling segments, allowing multiple processes to map the same file or anonymous region (via MAP_ANON in later extensions) for , with the kernel managing page faults and coherency through the virtual memory subsystem.

Windows and Other OS Evolutions

Memory-mapped files, known as general memory-mapped files (GMMF) in Windows, were introduced with Windows NT in 1993, providing robust support for mapping files into virtual address spaces through the Win32 API functions CreateFileMapping and MapViewOfFile. These APIs enable the creation of file-mapping objects that can represent files larger than physical memory, with dynamic expansion capabilities by specifying a maximum size exceeding the current file length in the CreateFileMapping call; upon writing to the extended region, the underlying file grows accordingly. The evolution of memory mapping in Windows addressed significant limitations in earlier systems like and 16-bit Windows, which lacked management and relied on rudimentary shared global memory blocks via flags such as GMEM_SHARE, restricting scalability and inter-process sharing. Full implementation arrived with the Win32 subsystem in , leveraging -level section objects—also called file-mapping objects—to facilitate shared mappings across processes, where multiple views of the same section can be mapped into different address spaces for efficient data interchange without explicit copying. A key feature in Windows is the ability to map views beyond the current file size, particularly for sparse files on NTFS, where unaccessed regions are not allocated on disk until written to, enabling efficient handling of large, irregularly accessed files by treating gaps as zero-filled virtual space. Beyond Windows, memory mapping concepts persisted in other systems, such as OpenVMS—a successor to the TOPS-20 operating system—which employed global sections to map files or pageable memory into shared virtual address spaces, supporting both private and global access modes for process communication and file-backed paging. Early Linux kernels adopted similar functionality in the early 1990s through the mmap system call, with initial support appearing in kernel version 0.98.2 in 1992 and improving in subsequent releases to provide POSIX-compliant file and anonymous mappings. This development paralleled Unix implementations like mmap, extending memory mapping to open-source Unix-like environments.

Advantages

Performance Enhancements

Memory-mapped files enable I/O by directly mapping file contents into a process's , eliminating the data copies between buffers and user-space buffers that occur in traditional read or write system calls. This reduces CPU overhead from memory copying and context switches, allowing applications to treat file as simple memory operations. Demand paging further enhances efficiency in memory-mapped files, as the operating system loads only the pages accessed by the application , rather than pre-loading the entire file into memory. This minimizes initial startup time and memory usage for large files, where only relevant portions are brought into via page faults, optimizing for sparse or patterns. Integration with the OS page cache provides additional performance gains, as mapped file regions leverage the system's unified caching mechanism, keeping frequently accessed data in physical memory for subsequent reads without disk I/O. Benchmarks demonstrate these benefits: for cached sequential reads, memory mapping can achieve up to 3x higher (e.g., 200 MB/s vs. 62 MB/s for traditional file reads on certain Unix systems) compared to standard read calls, though results vary by workload and hardware due to factors like handling. In microbenchmarks on 4 GB files, optimized memory mapping reduces I/O time to approximately 1.02 seconds, comparable to or slightly better than read calls at 1.06 seconds, while default implementations may incur higher latency from unoptimized paging.

Programming Simplicity

Memory-mapped files simplify programming by abstracting file access into direct memory operations, allowing developers to treat the file as a contiguous array in the process's address space and use standard pointer arithmetic or array indexing for reading and writing data. This eliminates the need for repetitive system calls like lseek(), read(), or write(), which are required in traditional file I/O to navigate and transfer data in chunks. Instead of managing offsets and buffer sizes manually, programmers can operate on the mapped region as if it were native memory, streamlining code for tasks involving large or sequentially accessed files. This abstraction also reduces common sources of errors, such as buffer overflows, misalignment issues, or incorrect calculations, by offloading data transfer and paging to the operating system . Synchronization challenges related to explicit I/O buffers are minimized, as the mapped memory integrates seamlessly with the program's existing memory model without requiring custom allocation or deallocation logic. To illustrate, consider processing a large in . With traditional I/O using fread(), the code involves opening the , allocating a , and looping over reads while handling partial reads and errors:
FILE *fp = fopen("data.bin", "rb");
char buffer[BUFSIZ];
size_t bytes_read;
while ((bytes_read = fread(buffer, 1, BUFSIZ, fp)) > 0) {
    // Process buffer [data](/page/Data), e.g., for (size_t i = 0; i < bytes_read; i++) { process(buffer[i]); }
    // Handle potential errors or [end-of-file](/page/End-of-file)
}
fclose(fp);
free(buffer);  // If dynamically allocated
In contrast, using mmap() maps the entire file (or a portion) into , enabling direct indexing without loops for or management:
int fd = open("data.bin", O_RDONLY);
off_t length = lseek(fd, [0](/page/0), SEEK_END);
void *ptr = mmap(NULL, length, PROT_READ, MAP_SHARED, fd, [0](/page/0));
if (ptr != MAP_FAILED) {
    // [Process](/page/Process) directly, e.g., for (off_t i = [0](/page/0); i < length; i++) { process(((char*)ptr)[i]); }
    munmap(ptr, length);
}
close(fd);
This mapping approach requires fewer lines of code and avoids explicit error-prone handling. For multi-threaded applications, memory-mapped files further enhance simplicity by providing inherent shared access to the mapped region across threads within the same process, as threads naturally share the . This allows concurrent reads or coordinated writes without the overhead of inter-thread communication primitives solely for I/O, treating the file-backed memory as a unified visible to all threads.

Types of Mappings

Persistent Mappings

Persistent mappings in memory-mapped files refer to configurations where the mapped is shared and modifications directly affect the underlying file on disk, ensuring durability beyond the lifetime of the mapping process. These mappings are typically established using flags such as MAP_SHARED in systems, which allow updates to the mapped area to be visible to other processes accessing the same file and to propagate changes to the persistent storage. In Windows environments, equivalent functionality is achieved through file-backed memory-mapped files created via APIs like CreateFileMapping or the .NET MemoryMappedFile.CreateFromFile, where the mapping is tied to an existing file handle. The behavior of persistent mappings ensures that writes to the mapped memory are eventually flushed to the disk file, providing a mechanism for long-term . In Unix systems, changes are carried through to the file automatically under MAP_SHARED, but precise control over —such as immediate or asynchronous flushing—is managed via the msync() system call to guarantee before unmapping or process exit. Similarly, in Windows, modifications to the mapped view update the source file upon closure of the last referencing process, without requiring explicit flushing in many cases, though APIs like FlushViewOfFile can enforce immediate persistence. This design makes persistent mappings suitable for scenarios requiring durable storage, as the file remains updated even after process termination or system restarts, provided has been properly invoked to avoid from caching. Persistent mappings are commonly employed for applications needing reliable, file-based , such as database engines that treat large datasets as mappable files for efficient querying and updates, or system configuration stores that maintain state across sessions. For instance, storage systems leveraging memory-mapped files for object benefit from the unified memory-file , enabling seamless data durability in distributed environments. These use cases highlight their role in ensuring over process lifecycles, with proper syncing preventing inconsistencies during failures.

Non-Persistent Mappings

Non-persistent mappings, often referred to as private mappings, involve creating a region that maps to a file but does not propagate modifications back to the underlying . In operating systems, these are typically established using the MAP_PRIVATE flag with the mmap() , which implements a (COW) mechanism: initial reads access the file directly, but any write operation triggers the creation of a private copy of the affected page in the process's , leaving the original file unchanged. This approach ensures that updates remain isolated to the mapping and are not visible to other processes or persisted to disk. The behavior of non-persistent mappings makes them particularly suitable for scenarios where read-only access or temporary in-memory edits are required without risking alteration of the source , such as in tools that parse large datasets for processing or validation. For instance, a might map a privately to experiment with modifications in before deciding whether to save changes separately. Unlike shared mappings, which synchronize changes across processes and to the file, private mappings prioritize to prevent unintended side effects. A key limitation of non-persistent mappings is the absence of automatic ; any changes made during the mapping's lifetime are discarded upon unmapping with munmap(), with no option to flush them to the original file without additional explicit handling. This design enforces their temporary nature but requires developers to manage persistence through alternative means if needed. In Windows, equivalent functionality is provided via the MapViewOfFile() function with the FILE_MAP_COPY access right, which also employs semantics: writes result in private page copies that do not affect the mapped file, ensuring the original remains unmodified. These mappings are commonly used in applications needing process-specific , such as secure of sensitive files where integrity must be preserved.

Disadvantages

Memory and Resource Overhead

Memory-mapped files impose significant memory pressure on a process because the entire mapped region is reserved in the process's , regardless of whether all pages are physically resident in . This reservation counts toward the process's virtual memory limit, potentially leading to failures when creating additional mappings if the limit is exceeded, as enforced by mechanisms like RLIMIT_DATA on systems. For instance, on 32-bit architectures, the total virtual address space is constrained to around 4 , limiting the aggregate size of all mappings. Even with demand paging, where pages are loaded only on access, the upfront reservation can fragment the and complicate for applications handling multiple large files. Large memory mappings exacerbate risks of and thrashing in environments with insufficient physical . When is oversubscribed, the operating system may evict frequently accessed pages from memory-mapped regions to disk, causing excessive page faults and I/O operations that degrade performance. This thrashing occurs as the system spends more time managing than executing application code, particularly for mappings exceeding available , such as multi-gigabyte files in workloads. To mitigate this, some systems allow flags like MAP_NORESERVE on OSes to avoid pre-reserving swap space, though this increases the risk of segmentation faults during writes if is unavailable. Each memory mapping consumes kernel resources, including virtual memory areas (VMAs) that track the mapping in the process's . On , the number of VMAs is limited by /proc/sys/vm/max_map_count, defaulting to , beyond which new mappings fail with ENOMEM; excessive mappings can thus exhaust this quota even if physical memory is available. Additionally, creating a mapping requires an open , which, while closable post-mapping without invalidating the region, still incurs temporary resource use and contributes to per-process open file limits. Due to page-level granularity—typically 4 on x86 systems—memory mappings introduce overhead for small files, where the last incomplete results in slack space allocation. For example, mapping a 1 file reserves a full 4 page in , wasting 3 per such mapping, which accumulates in applications processing many tiny files like indexes. This alignment requirement stems from page sizes and ensures efficient but amplifies inefficiency for non-page-aligned data sizes.

Portability and Compatibility Issues

Memory-mapped files exhibit significant variations across operating systems, complicating portability. In -compliant systems, the mmap function provides a single to map a into , specified by parameters including hint, , modes (e.g., PROT_READ, PROT_WRITE), flags (e.g., MAP_SHARED for shared modifications or MAP_PRIVATE for ), , and offset. Conversely, Windows employs a two-step process: first creating a object with CreateFileMapping, then a view using MapViewOfFile, which takes a to the mapping object, desired (e.g., FILE_MAP_READ, FILE_MAP_WRITE), offset components, and byte count, but lacks direct equivalents to flags like MAP_SHARED, instead achieving sharing through the mapping passed between processes. These differences in invocation, parameters, and semantics—such as 's optional support for MAP_FIXED versus Windows' granularity requirements—require conditional compilation or abstraction layers for cross-platform code. Operating system limitations further hinder compatibility, particularly in older or constrained environments. While mmap has been supported in Linux since kernel version 0.98.2 in 1992, 32-bit systems impose strict file size constraints on mappings, often limited to 2 GB due to virtual address space boundaries, necessitating special handling like open64 for larger files in POSIX environments. Similarly, Windows on 32-bit architectures restricts individual memory-mapped views to 2 GB, even if the underlying file exceeds this, requiring multiple views for larger data sets. These address space limitations persist in legacy 32-bit deployments, contrasting with 64-bit systems where mappings can span terabytes, but demand careful size management to avoid failures. Security concerns arise from memory mapping's interaction with system policies, potentially exposing vulnerabilities. In , overcommitment—enabled by default via /proc/sys/vm/overcommit_memory=0—allows to allocate beyond physical availability, assuming delayed usage; however, upon page access, if memory is exhausted, the killer may terminate processes based on a badness score factoring usage and adjustability, risking unintended or denial-of-service. Access control relies on underlying file permissions: mmap requires the file descriptor to be opened with at least read access, and write protections (PROT_WRITE) demand matching file write permissions, enforcing (DAC) without additional mapping-specific ACLs. On Windows, mappings inherit file permissions but use dedicated security descriptors on the file mapping object, supporting granular rights like FILE_MAP_READ or FILE_MAP_WRITE via ACLs, audited through functions such as SetSecurityInfo. To mitigate these portability issues, libraries provide abstraction layers. Boost.Interprocess, for instance, emulates portable using memory-mapped files, unifying mmap and Windows CreateFileMapping/MapViewOfFile interfaces to enable cross-platform without direct exposure. This approach ensures consistent behavior for mappings, handling flag equivalents and error conditions transparently across and Windows systems.

Applications

File Handling and I/O

Memory-mapped files facilitate efficient to files by mapping the file content into , allowing applications to use pointer arithmetic or array-like indexing instead of explicit operations in traditional I/O . This is particularly beneficial for processing files or , where data is accessed in a linear fashion, minimizing the overhead of repeated positioning calls. In , for instance, the package enables sequential subsetting of mapped files as native vectors, achieving high throughput through OS-managed paging and reducing garbage collection compared to loading data fully into . For patterns, memory-mapped files provide direct byte-level addressing, treating the file as a contiguous memory block for non-sequential reads and writes, which is ideal for files requiring scattered access. This approach avoids the of seek and read system calls for each operation, as the OS handles paging transparently. In .NET, views created via CreateViewAccessor support this by allowing byte-level modifications to persisted files without buffering the entire content. Handling large files represents a key strength of memory-mapped I/O, enabling operations on terabyte-scale datasets without loading them entirely into , as only accessed pages are brought into physical by the OS. Sparse mappings further optimize this for files with irregular access, where untouched regions consume no resources; for example, implementations can map 8 virtual files using just megabytes of and disk for sparse writes. On 64-bit systems, such mappings can extend to 256 TB, supporting applications that process massive streams efficiently. In image processing, memory-mapped files allow direct manipulation of data in formats, such as , by mapping the file and accessing RGB values at arbitrary offsets for operations like color adjustment, avoiding the need to allocate full in-memory buffers for high-resolution images. This technique leverages views to brighten or modify specific regions, with the OS ensuring during writes. Memory mapping enhances performance in these scenarios by eliminating explicit I/O calls after initial setup, relying on mechanisms for efficient caching.

Inter-Process Communication

Memory-mapped files enable (IPC) by allowing multiple processes to map the same region of memory, facilitating efficient data exchange without explicit copying. In systems, processes can create a shared memory object using shm_open(), which returns a to a named object that serves as a for mapping the same memory region via with the MAP_SHARED flag. This setup permits unrelated processes to access the shared segment concurrently, where modifications by one process are visible to others, provided proper is employed. For pure IPC without a persistent backing file, mappings can be used, particularly in scenarios where data does not need to survive process termination. In POSIX-compliant systems, mmap() with the MAP_ANONYMOUS and MAP_SHARED flags allocates a region directly, bypassing file descriptors entirely; this is suitable for related processes (e.g., parent-child after fork()) but requires naming mechanisms like shm_open() for unrelated processes to join the segment. Such mappings avoid disk I/O overhead, making them ideal for high-speed data transfer in temporary collaborations. Synchronization is essential in shared mappings to prevent race conditions, as concurrent access can lead to without coordination. provides process-shared mutexes via pthread_mutex_init() with the PTHREAD_PROCESS_SHARED attribute, placing the mutex in the region to enforce across processes. Alternatively, named semaphores created with sem_open() can signal availability and control access, ensuring atomic operations on shared data. These primitives must be explicitly managed, as the operating system does not provide automatic barriers for memory-mapped regions. A common application is the producer-consumer pattern in client-server architectures, where a process writes data to the shared mapping while a reads it, using semaphores to manage fullness and . For instance, in a bounded implementation, semaphores mutex (initialized to 1 for exclusion), full (0 for empty slots), and empty (buffer size for available slots) coordinate access: the waits on empty and mutex before adding an item and signals full, while the reverses the to avoid . This pattern leverages for low-latency exchange, as seen in systems handling real-time data streams.

Database and Large Data Processing

In database engines such as , memory-mapped files enable memory-like access to on-disk indexes by directly mapping database pages into the process's virtual address space, allowing queries to fetch data without explicit read system calls and kernel-user space copies. This approach is particularly beneficial for I/O-intensive read operations, where the operating system's handles paging transparently, reducing overhead for index lookups and scans. Similarly, utilizes memory-mapped files for its immutable sorted string tables (SSTables), mapping these files to improve random read performance by leveraging the OS for frequent key-value lookups without loading entire files into . In processing frameworks like , memory-mapped files facilitate efficient handling of large datasets by mapping input blocks from disk into during reads, avoiding unnecessary copies and enabling columnar storage formats such as to process petabyte-scale data without fully loading it into heap . This integration supports distributed query execution on clusters, where mapped files allow executors to access data lazily, minimizing pressure for transformations and aggregations on massive datasets. Modern systems extend memory-mapped techniques through engines like , which supports memory-mapped indexes by mmapping entire SSTables for reads via the allow_mmap_reads option, enabling efficient access to on-disk data structures without full in-memory loading even at petabyte scales. This is crucial for read-heavy workloads in distributed databases, where mapped indexes reduce the need to cache all metadata in while maintaining low-latency point lookups and range scans.

Platform Support

Unix-like Operating Systems

In operating systems, memory-mapped files are primarily supported through the standard, which defines the core system calls for mapping files into a process's . The mmap() function establishes a mapping between a process's and a file or device, allowing to file contents without explicit read or write system calls. This mapping is specified by parameters including the starting address (addr), length (len), protection flags such as PROT_READ for read-only access or PROT_WRITE for read-write access, mapping flags like MAP_SHARED for shared changes or MAP_PRIVATE for private copies, and an offset into the file. The munmap() function unmaps the region previously established by mmap(), releasing the virtual address space, while msync() synchronizes the mapped memory with the underlying file, ensuring changes are written to disk if the MS_SYNC flag is used. These APIs are implemented consistently across , macOS, and BSD variants, with mmap() returning a pointer to the mapped area or MAP_FAILED on error. Linux extends these POSIX interfaces with advanced kernel features for optimizing memory mappings. Support for huge pages, also known as Huge TLB pages, allows mappings larger than the standard 4 KiB page size—typically 2 or 1 GiB—to reduce (TLB) overhead and improve performance for large files. Applications can request huge page mappings by specifying the MAP_HUGETLB flag in mmap(), provided the kernel is configured with huge page support via boot parameters like hugepagesz=2M. Additionally, the madvise() enables processes to provide hints to the kernel about expected access patterns for mapped regions, such as MADV_WILLNEED to prefetch pages or MADV_SEQUENTIAL for linear access, which can enhance caching and paging efficiency. Despite these capabilities, memory mappings in systems have limitations tied to support. For instance, mappings on (NFS) mounts may fail or behave inconsistently if the mount lacks proper options like noac or if the NFS version does not fully support coherent caching, as the cannot guarantee updates across network shares. Regular files must support seeking to arbitrary offsets, and the offset parameter for the mapping must be a multiple of the page size. Enhancements in versions 5.x and later, including the 6.x series as of 2025, have improved transparent huge pages (THP) for memory mappings, building on their introduction in kernel 2.6.38. THP automatically promotes contiguous base pages to 2 MiB huge pages during allocation or faulting, with optimizations in 5.x series including better collapse heuristics via khugepaged and support for file-backed mappings through madvise hints like MADV_HUGEPAGE. These updates, such as refined scanning in kernel 5.0 and later, reduce allocation latency and memory fragmentation for mmap-based workloads without requiring explicit huge page configuration. In macOS and BSD systems, while compliance ensures core functionality, huge page support is more limited, often relying on standard page sizes without the automatic THP mechanisms found in .

Windows Operating Systems

In Windows operating systems, memory-mapped files are implemented through the , which provides functions to create, map, and manage file mapping objects, also known as objects, for associating file contents with . These objects maintain the association between a file and a view of its data in process address space, enabling efficient and access across processes. The primary API for creating a file mapping object is CreateFileMapping, which takes a to a file (or INVALID_HANDLE_VALUE for pagefile-backed mappings) and specifies the mapping size, protection attributes, and an optional name for sharing. This function returns a handle to the section object, which can be used by multiple processes for if named. To access the mapped data, MapViewOfFile is called with the section , defining the offset and length of the view to map into the calling process's . Views can be mapped with various access protections, such as read-only or read-write, and the system handles paging automatically. When finished, UnmapViewOfFile releases the view from the , and the section handle is closed with CloseHandle to decrement its reference count. Security for file mapping objects is managed through security descriptors specified in the SECURITY_ATTRIBUTES structure passed to CreateFileMapping. These descriptors define access control lists (ACLs) that control permissions, such as read, write, or execute, for processes attempting to open or map the section; by default, ACLs derive from the creator's token. Additional functions like SetNamedSecurityInfo allow modifying these descriptors post-creation to enforce granular access rights, including standard rights like DELETE or WRITE_DAC. Windows supports memory-mapped executables by loading (PE) files into process using section objects, where the loader creates mappings for code, data, and resource sections without requiring explicit file opens. This mechanism ensures that executable images are efficiently paged and shared, with sections aligned to page boundaries for optimal performance. Full support for these APIs has been available since , with enhancements and stability in features like large-page support introduced in and later. In modern and later, (UWP) applications face restrictions: file mappings are by default limited to processes within the same package, requiring full-trust capabilities for broader inter-process sharing. For scenarios not involving files, such as reserving large virtual address spaces without immediate physical or pagefile backing, VirtualAlloc with the MEM_RESERVE flag can allocate regions up to the process's virtual limit, allowing subsequent commits or mappings as needed.

Other Platforms and Libraries

In systems and operating systems (RTOS), memory-mapped files are supported through specialized mechanisms rather than standard interfaces. For instance, provides memory mapping capabilities via hardware-specific ports and linker configurations that allow direct access to memory regions, though it lacks native support due to its lightweight design for microcontrollers without full management. On , which serves as an Linux variant, the ashmem (Android Shared Memory) allocator enables anonymous shared memory regions that can be mapped into process address spaces using , facilitating efficient and data sharing while allowing the to reclaim memory under pressure. Cross-platform libraries abstract operations to ensure portability across operating systems. Python's module provides a high-level for files into , treating them as mutable byte arrays or file-like objects, which leverages the underlying OS's for efficient I/O on supported platforms. Similarly, Java's New I/O () includes MappedByteBuffer, a direct byte buffer that a file region into via FileChannel., enabling and modifications that are automatically synchronized back to the file. These libraries promote conceptual uniformity, allowing developers to handle large files without loading them entirely into RAM, though they inherit platform-specific behaviors for modes like read-only or read-write. On other operating systems, memory-mapped files integrate with unique protocols or face security-imposed limitations. Plan 9 from Bell Labs uses the 9P protocol for distributed file access, where clients can map remote files into local memory spaces, treating network resources as local files for seamless mapping operations. In iOS, memory-mapped files are restricted for security reasons, with the operating system enforcing protections against executable or writable mappings outside designated regions to prevent exploits, and limiting virtual memory usage to conserve resources on mobile devices. Recent developments in runtimes have explored memory-mapped interfaces through the WebAssembly System Interface (WASI). Since , proposals like an MVP for in WASI aim to enable file mappings within sandboxed WebAssembly modules, allowing portable access to host file systems without direct OS calls, though implementations remain experimental and focused on emulated behaviors for compatibility.

References

  1. [1]
    File Mapping - Win32 apps - Microsoft Learn
    Jan 7, 2021 · File mapping allows the process to use both random input and output (I/O) and sequential I/O. It also allows the process to work efficiently ...
  2. [2]
    mmap(2) - Linux manual page - man7.org
    mmap() creates a new mapping in the virtual address space of the calling process. The starting address for the new mapping is specified in addr. The length ...
  3. [3]
    Understanding memory mapping - IBM
    Memory mapped files provide a mechanism for a process to access files by directly incorporating file data into the process address space.
  4. [4]
    Overview of Memory-Mapping - MATLAB & Simulink - MathWorks
    Memory-mapping is a mechanism that maps a portion of a file, or an entire file, on disk to a range of addresses within an application's address space.
  5. [5]
    mmap
    DESCRIPTION. The mmap() function shall establish a mapping between an address space of a process and a memory object. The mmap() function shall be supported ...
  6. [6]
    [PDF] Virtual Memory - Carnegie Mellon University
    Jun 12, 2025 · □ Nifty things virtual memory makes possible. ▫ Paging/swapping (disk as extra RAM). ▫ Memory-mapped files (RAM as cache for disk). ▫ Copy ...
  7. [7]
    mmap
    The mmap() function shall establish a mapping between a process' address space and a file, shared memory object, or [Option Start] typed memory object.
  8. [8]
    Origins and Development of TOPS-20 - OPOST.COM Home
    TOPS-20 was first announced as a DEC product and shipped in January, 1976. Development had started in 1973 based on TENEX[1], an operating system for the PDP- ...Missing: Equipment | Show results with:Equipment
  9. [9]
    TOPS-20 - Computer History Wiki
    Jun 28, 2024 · Date Released: 1976. TOPS-20 is an operating system for the PDP-10, DECSYSTEM-20. It was descended from TENEX, an operating system for a ...
  10. [10]
    [PDF] TOPS-20 Monitor Calls Reference Manual - Bitsavers.org
    Dec 1, 1982 · ... 10. •. PDVOP%. PEEK. PLOCK. PMAP. PMCTL. PPNST. PRARG. PSOU'l'. RCDIR. RCM. RCUSR. RCVIM. RCVOK%. RDTTY. RELD. RELSQ. RESET. RFACS. RFBSZ. RFCOC.
  11. [11]
    [PDF] The Process File System and Process Model in UNIX System V
    We describe the process file system /proc in UNIX System V Release 4 and its relationship to the UNIX process model abstraction. /proc began as a debugger ...
  12. [12]
    [PDF] Memory-Mapped Files in Windows NT Simplify File ... - Jacob Filipp
    CreateFile. Opens a file on the disk to prepare it for file mapping. CreateFileMapping. Creates a file-mapping object from a disk file or an in-memory file.
  13. [13]
    Section Objects and Views - Windows drivers - Microsoft Learn
    Feb 21, 2025 · A section object represents a section of memory that can be shared. A process can use a section object to share parts of its memory address space.
  14. [14]
    Creating a File Mapping Object - Win32 apps | Microsoft Learn
    Sep 23, 2025 · Learn how to create a file mapping object in Windows by calling CreateFile and CreateFileMapping functions. Step-by-step guide with code ...
  15. [15]
    Sparse Files - Win32 apps - Microsoft Learn
    Jan 7, 2021 · Support for sparse files is introduced in the NTFS file system as another way to make disk space usage more efficient. When sparse file ...
  16. [16]
    Windows NT and VMS: The Rest of the Story - ITPro Today
    Both NT and VMS rely heavily on memory-mapped files, especially for mapping the code for executing applications and implementing copy-on-write functionality ...
  17. [17]
    [PDF] Efficient Memory Mapped File I/O for In-Memory File Systems | USENIX
    The paper proposes map-ahead, mapping cache, and extended madvise techniques to reduce memory mapping overhead, improving performance of memory mapped file I/O.
  18. [18]
    [PDF] lmbench: Portable Tools for Performance Analysis - USENIX
    Ideally, File mmap performance should approach Memory read performance, but mmap is often dramatically worse. Judging by the results, this looks to be a ...
  19. [19]
    [PDF] OSPP: Address Translation
    memory in limited physical memory? – Demand-paged virtual memory. – Memory-mapped files. • ... • Programming simplicity, esp for large files ... – Memory-mapped ...
  20. [20]
    COS 318: Operating Systems Virtual Memory Design ... - cs.Princeton
    ◇ Memory-mapped files. ○. Open file as a ... ◇ Programming simplicity. ◇ Efficient for large ... Memory-mapped Files and Demand-Paged VM. ◇ Can go ...
  21. [21]
    3.4. Shared Memory With Memory-mapped Files
    Memory-mapped files allow for multiple processes to share read-only access to a common file. As a straightforward example, the C standard library ( glibc.so ) ...Missing: definition | Show results with:definition
  22. [22]
    Memory-Mapped Files - .NET - Microsoft Learn
    Dec 14, 2022 · A memory-mapped file contains the contents of a file in virtual memory. This mapping between a file and memory space enables an application, including multiple ...Examples · Persisted Memory-Mapped... · Programming With...
  23. [23]
    [PDF] Good Old-Fashioned Persistent Memory - USENIX
    Nov 26, 2019 · Persistent memory programming on conventional hardware is possible, thanks to mmap() and a few tricks that don't get as much attention as they ...<|control11|><|separator|>
  24. [24]
  25. [25]
    MapViewOfFile function (memoryapi.h) - Win32 apps | Microsoft Learn
    Oct 30, 2024 · Maps a view of a file mapping into the address space of a calling process. To specify a suggested base address for the view, use the MapViewOfFileEx function.Syntax · Parameters · RemarksMissing: documentation | Show results with:documentation
  26. [26]
    The phaseout of the mmap() file operation - LWN.net
    Sep 25, 2025 · The mmap() method, in particular, is invoked when user space calls the mmap() system call to map the object behind a file descriptor into its ...Missing: resources | Show results with:resources
  27. [27]
    Memory-mapped I/O (The GNU C Library)
    On modern operating systems, it is possible to mmap (pronounced “em-map”) a file to a region of memory. When this is done, the file can be accessed just like an ...
  28. [28]
    Taming the OOM killer - LWN.net
    Feb 4, 2009 · The OOM killer kicks in when memory has been overcommitted through COW. Two processes are sharing the same memory region and one of them ...Who's Bad? · Shifting Oom-Killing Policy... · Low Memory In Embedded...<|separator|>
  29. [29]
    File Mapping Security and Access Rights - Win32 apps
    Jan 7, 2021 · Mapping a copy-on-write view of a file-mapping object requires the same access as mapping a read-only view. The FILE_MAP_COPY flag is not an ...Missing: private | Show results with:private
  30. [30]
    None
    Nothing is retrieved...<|control11|><|separator|>
  31. [31]
    [PDF] mmap: Memory Mapped Files in R
    Aug 1, 2011 · Memory mapped files (mmap files) leverage the operating system demand-based paging infrastructure to move data from disk to memory as needed, ...Missing: zero- | Show results with:zero-
  32. [32]
    Efficient Memory Mapping for Terabyte Sparse Files in Java
    Mapping large areas of memory avoids having to know in advance how much memory we need or having to resize the memory mappings while in use, while accessing the ...
  33. [33]
    shm_open
    The shm_open() function shall establish a connection between a shared memory object and a file descriptor. It shall create an open file description.
  34. [34]
    mmap
    ### Summary: Using mmap with Shared Memory Objects for Inter-Process Communication
  35. [35]
  36. [36]
    pthread_mutex_destroy
    ### Summary: Using `pthread_mutex` with `PTHREAD_PROCESS_SHARED` for Inter-Process Mutex in Shared Memory
  37. [37]
    8.3. Producer-Consumer Problem - Computer Science - JMU
    The producer-consumer problem involves threads creating results (producers) and taking results (consumers) from a shared queue. Consumers cannot repeatedly ...
  38. [38]
    [PDF] Principles of Operating Systems
    Consumer waits for new item, producer waits if buffer is full. Producer and Consumer must synchronize. Shared memory solution to the bounded-buffer problem ...
  39. [39]
    Memory-Mapped I/O - SQLite
    Apr 18, 2022 · Memory-mapped I/O in SQLite directly accesses disk content using xFetch(), skipping copies, potentially speeding up I/O intensive operations.
  40. [40]
    LevelDB Explained - Posix File Operation Details
    Aug 2, 2024 · Memory mapping allows the operating system to utilize page caches, which can significantly improve the performance of frequent reads, especially ...Sequential File Reading · Random File Reading · Sequential File Writing
  41. [41]
    Configuration - Spark 4.0.1 Documentation - Apache Spark
    ... Spark memory maps when reading a block from disk. Default unit is bytes, unless specified otherwise. This prevents Spark from memory mapping very small blocks.
  42. [42]
    IO · facebook/rocksdb Wiki - GitHub
    Memory Mapping. options.allow_mmap_reads and options.allow_mmap_writes make RocksDB mmap the whole data file while doing read or write, respectively. The ...
  43. [43]
    SQLite performance tuning - phiresky's blog
    Jul 31, 2022 · Uses memory mapping instead of read/write calls when the database is < mmap_size in bytes. Less syscalls, and pages and caches will be managed ...Missing: benchmark | Show results with:benchmark
  44. [44]
    HugeTLB Pages — The Linux Kernel documentation
    Users can use the huge page support in Linux kernel by either using the mmap system call or standard SYSV shared memory system calls (shmget, shmat). First ...
  45. [45]
    madvise(2) - Linux manual page - man7.org
    For file-mapped memory —including tmpfs (see tmpfs(2))— the mapping must also be naturally hugepage-aligned within the file. Additionally, for file-backed ...Synopsis Top · Description Top · Errors Top
  46. [46]
    Transparent Hugepage Support - The Linux Kernel documentation
    Transparent HugePage Support (THP) is an alternative mean of using huge pages for the backing of virtual memory with huge pages.Transparent Hugepage Support · Sysfs · Hugepages In Tmpfs/shmemMissing: 5. enhancements
  47. [47]
    Mac OS X Manual Page For mmap(2) - Apple Developer
    This document is a Mac OS X manual page. Manual pages are a command-line technology for providing documentation. You can view these manual pages locally ...
  48. [48]
    CreateFileMappingW function (memoryapi.h) - Win32 apps
    Jul 27, 2022 · Creates or opens a named or unnamed file mapping object for a specified file. To specify the NUMA node for the physical memory, see CreateFileMappingNuma.
  49. [49]
    Creating Named Shared Memory - Win32 apps - Microsoft Learn
    Jul 30, 2024 · The first process creates the file mapping object by calling the CreateFileMapping function with INVALID_HANDLE_VALUE and a name for the object.
  50. [50]
    Closing a File Mapping Object - Win32 apps | Microsoft Learn
    Jan 7, 2021 · When a process has finished with the file mapping object, it should destroy all file views in its address space by using the UnmapViewOfFile function for each ...
  51. [51]
    CreateFileMappingA function (winbase.h) - Win32 apps
    Jul 26, 2022 · The access control lists (ACL) in the default security descriptor for a file mapping object come from the primary or impersonation token of the ...
  52. [52]
    Executable Images - Windows drivers - Microsoft Learn
    Dec 14, 2021 · Executable files are loaded into the address space of a process using a memory mapped image file. The file itself is not required to be opened.
  53. [53]
    Inside Windows: Win32 Portable Executable File Format in Detail
    This article, the first of a two-part series, looks at the changes to the PE format that have occurred over the last few years, along with an overview of the ...
  54. [54]
    Interprocess communication (IPC) - UWP applications
    Nov 1, 2022 · By default, file mappings in packaged applications are supported only between processes within the same package, unless a process is full trust.
  55. [55]
    VirtualAlloc function (memoryapi.h) - Win32 apps - Microsoft Learn
    Feb 5, 2024 · Reserves a range of the process's virtual address space without allocating any actual physical storage in memory or in the paging file on disk.Memory Protection Constants · VirtualAllocEx · VirtualFree function
  56. [56]
    RM44L520: Memory mapping issue with FreeRTOS - TI E2E
    Apr 4, 2022 · You can use the link cmd file of RM44L520PGE for RM44L520PZ. Both package has the same size of RAM and Flash. Did you follow the procedure to ...TDA4VM: Does psdk8.00 support modify freertos memory map with ...FreeRTOS functions use more SRAM than is available - TI E2EMore results from e2e.ti.com
  57. [57]
    Maping a pointer at a particular address. - Kernel
    Jul 8, 2019 · FreeRTOS does not have a direct OS-level function to map a pointer to a specific address. It's suggested to map directly to hardware, using a ...
  58. [58]
    Memory - NDK - Android Developers
    Nov 19, 2024 · Create a shared memory region and returns a file descriptor. The resulting file descriptor can be mapped into the process' memory using mmap(2) with PROT_READ ...
  59. [59]
    BitUnmap: Attacking Android Ashmem - Google Project Zero
    Dec 1, 2016 · In this blog post we'll explore the ashmem shared memory interface provided by Android and see how false assumptions about its internal operation can result in ...
  60. [60]
    Implementing Ashmem to share data between processes - Jinhan Hu
    Jan 8, 2021 · I turn to Ashmem, Android's version of shared memory, to directly share large chunks of memory with low level system support.
  61. [61]
    mmap — Memory-mapped file support — Python 3.14.0 ...
    Memory-mapped file objects behave like both bytearray and like file objects. You can use mmap objects in most places where bytearray are expected.
  62. [62]
    MappedByteBuffer (Java Platform SE 8 ) - Oracle Help Center
    A direct byte buffer whose content is a memory-mapped region of a file. Mapped byte buffers are created via the FileChannel.map method.
  63. [63]
    Python mmap: Improved File I/O With Memory Mapping
    In this tutorial, you'll learn how to use Python's mmap module to improve your code's performance when you're working with files.
  64. [64]
    The 64-bit Standalone Plan 9 File Server
    This paper is a revision of Thompson's The Plan 9 File Server, and describes the structure and the operation of the new 64-bit Plan 9 file servers.
  65. [65]
    v9fs: Plan 9 Resource Sharing for Linux
    v9fs is a Unix implementation of the Plan 9 9p remote filesystem protocol. This software was originally developed by Ron Minnich <rminnich@sandia.gov> and Maya ...Missing: memory | Show results with:memory
  66. [66]
    Operating system integrity - Apple Support
    Dec 19, 2024 · Executable mappings outside its part of the protected memory region. Writeable mappings inside its part of the protected memory region. Also at ...
  67. [67]
    Memory mapping limitations on iOS - JUCE Forum
    Apr 29, 2019 · Yes, there is a limit on iOS to how much virtual memory you can use. The system is quite limited compared to a desktop OS VM system. Only read-only pages can ...Missing: security | Show results with:security
  68. [68]
    Support for an MVP of mmap #304 - WebAssembly/WASI - GitHub
    Jul 29, 2020 · A simple emulated mmap in wasi-libc, that does malloc + read to copy the file contents into memory, so the main advantage here would be to let implementations ...