memory-mapped-file · shipping · also file-backed mmap, mmap file mapping

Memory-mapped file

A memory-mapped file exposes a file region through a process's virtual address space so its contents can be accessed with memory loads and stores.

Related Observatory article: Read on hesela.com

Mapping mode: MAP_SHARED permits file-backed updates; MAP_PRIVATE keeps modifications private through copy-on-write but does not promise a snapshot of later external changes. Closing the original descriptor leaves the mapping valid. [1]

File extent: mapping length does not grow the file. Whole pages beyond the object can raise SIGBUS; stores in the last partial page's unused tail do not extend it. [2] [1]

Persistence: background writeback can precede msync; synchronization is distinct from an atomic transaction. [3]

Sealed objects: memfd seals can restrict writes or resizing, but FUTURE_WRITE permits earlier shared writable mappings to keep writing. [5]

Partial-page tail: Linux warns that out-of-file bytes can remain visible after close and unmap; msync does not clear this tail on tmpfs. A Linux 6.8 probe with ext4 and tmpfs controls reproduced this without changing file contents or length. This is not evidence of persistence after reboot or portable tail clearing. [1] [4]

Change detection: a controlled shared-mapping test changed bytes without IN_MODIFY before final close; a writable-close event also occurred without changing bytes. [6]

Discard advice: native MADV_DONTNEED discarded private edits in controlled Linux tests, while POSIX_MADV_DONTNEED in glibc retained them. Interface and mapping mode determine the meaning of success; the tests measure bytes, not globally cold cache state or persistence. [7]

Retained access: on one Linux 6.8 host (ext4/tmpfs), an O_RDWR handle admitted a shared writable mapping after chmod 000 denied fresh opens; it wrote after descriptor close. This shows visibility, not crash durability. [14]

Relations

related
msync, File truncation, Page cache, Copy-on-write (storage), LMDB, Linux file sealing, Linux memfd_create, FastMap, inotify, MADV_DONTNEED, POSIX_MADV_DONTNEED, Memory locking (mlock), tmpfs, FlushViewOfFile, FlushFileBuffers, FlashMap, MADV_POPULATE_READ, MADV_POPULATE_WRITE

Advantages

  • Allows a file region to be addressed without an explicit read call for each access.

Limitations

  • Concurrent access and file resizing require an application-level coordination protocol.
  • MAP_PRIVATE and prefaulting do not establish a consistent file version; the external-writer experiment is a single-host observation, not a portable visibility guarantee.

Citations

  1. Linux man-pages: mmap(2), mapping modes and lifetime (accessed 2026-10-18)
  2. POSIX.1-2017: mmap(), object size and mapping boundaries (accessed 2026-10-17)
  3. POSIX.1-2017: msync(), completion and background writeback (accessed 2026-10-17)
  4. Hesela: Memory-mapped files: visibility, size and synchronization (accessed 2026-10-18)
  5. Linux man-pages: F_GET_SEALS / F_ADD_SEALS (accessed 2026-10-17)
  6. Hesela: What inotify misses: mapped writes and collapsed change events (accessed 2026-10-19)
  7. Hesela: When don't need discards private edits (accessed 2026-10-20)
  8. Hesela: Private page history is not a file version, six ordered Linux controls (accessed 2026-10-26)
  9. CPython v3.12.3: Unix new_mmap_object access-mode dispatch (accessed 2026-10-26)
  10. Hesela: compiler-dependent private-mapping reproduction, source, disassembly and two captures (accessed 2026-10-27)
  11. GCC: volatile accesses and limits of memory ordering (accessed 2026-10-27)
  12. Linux v6.8: do_mmap and FMODE_WRITE (accessed 2026-11-05)
  13. Linux v6.8: vm_mmap_pgoff security hook (accessed 2026-11-05)
  14. Hesela: retained local access after permission changes, native evidence and limits (accessed 2026-11-05)
  15. Hesela: permission-lifetime native observations, source receipts and MIT replay bundle (accessed 2026-11-05)
Sources & caveats
  • The POSIX citations refer to the 2017 edition; Linux-specific details are identified separately.
  • The companion experiment checks API behavior on one host, not power-loss survival.
  • The private-history supplement uses six controls on one Linux host. Prefaulting and read-only protection did not freeze clean mappings; pathname replacement preserved old-inode identity, not immutability. This is not a portable snapshot recipe or a database-engine bug.
  • CPython 3.12.3 on Unix maps ACCESS_READ to MAP_SHARED with PROT_READ; ACCESS_COPY selects private read/write mapping. This probe explicitly uses MAP_PRIVATE and PROT_READ; convenience-mode behavior was not separately measured. [9]
  • Compiler supplement: 24 repeated observations, two captures on Linux 6.8/ext4, GCC 13.3.0; not independent samples. The original writer helpers are reused. No universal compiler behavior or kernel fault tracing is claimed. [10]
  • Reproduction: in GCC 13.3.0 controls, plain C self-assignment lost its store at -O2 and read BB, while a volatile byte store retained AB. Emitted accesses matter; volatile supplies neither a snapshot nor synchronization. [10] [11]
  • Permission-lifetime evidence: nine scenarios, 108 recorded executions and 108 matching replay records; two compiler configurations, three repetitions per mount and one host, not independent fleet samples. No changed LSM policy, concurrent writer or power-loss experiment. [15]
  • Version consistency: after an ordered writer exited, a Linux probe read AB from a private mapping although file history was AA -> BA -> BB. A same-value store retained page 0; page 1 followed the file. [8]
  • Shared writable mapping admission checks file->f_mode and additional restrictions. The mapping security hook still applies; the experiment did not change LSM policy or test NFS. [12] [13]

JSON: /data/terms/memory-mapped-file.json

Published by Hesela team · Updated

Report a correction · Editorial standards · Reuse guidance

Cite this page
Hesela team. Memory-mapped file. Hesela. 2026-12-01. https://hesela.dev/terms/memory-mapped-file/