{
    "slug": "memory_mapped_files",
    "term": "Memory-Mapped Files",
    "category": "linux",
    "difficulty": "advanced",
    "short": "A file mapped directly into a process's virtual address space — reads and writes go through the OS page cache rather than read()/write() syscalls, enabling fast access to large files and shared memory between processes.",
    "long": "Memory mapping (mmap on Linux) creates a direct mapping between a file on disk and a region of virtual memory. When a process reads from that address range, the OS transparently loads the relevant page from disk via a page fault. Writes modify the page in memory and the OS flushes them to disk asynchronously. This eliminates the user-to-kernel copy that standard file I/O requires, reducing syscall overhead for large sequential reads. Multiple processes mapping the same file share the underlying pages, enabling efficient IPC (inter-process communication) without explicit message passing. PHP itself does not expose mmap directly, but it is used internally by opcache to share the compiled opcode cache between FPM workers — all workers read from the same shared memory region rather than each maintaining a private copy. SQLite uses mmap for its WAL-mode readers. PHP extensions can use mmap via FFI or C extensions for performance-critical work.",
    "aliases": [],
    "tags": [
        "linux",
        "systems",
        "memory",
        "performance",
        "ipc",
        "php-internals"
    ],
    "misconception": "Memory-mapped files are not always faster than read() — for small files or random single reads, the page fault overhead and TLB pressure can make mmap slower than a simple file_get_contents().",
    "why_it_matters": "PHP opcache uses mmap to share compiled bytecode across all FPM workers — understanding this explains why opcache.memory_consumption must be tuned to fit the entire codebase, and why a too-small value causes silent re-compilation.",
    "common_mistakes": [
        "Assuming mmap is always the fastest I/O method — random single-byte reads across a large mapping cause repeated page faults that are slower than buffered read().",
        "Mapping very large files on 32-bit systems — virtual address space is limited to ~3 GB, making large mappings impractical or impossible.",
        "Not accounting for dirty page writeback latency — a write to a mapped page is not on disk until the OS flushes it, which is asynchronous by default."
    ],
    "when_to_use": [
        "Use mmap for large sequential reads of files that exceed available RAM — the OS streams pages in and evicts them transparently.",
        "Use shared anonymous mappings for IPC between processes on the same host where a message queue would add unnecessary overhead.",
        "Tune opcache.memory_consumption based on your codebase size — it uses mmap internally and insufficient size causes re-compilation."
    ],
    "avoid_when": [
        "Avoid mmap for small files — the setup overhead and page table entries outweigh the benefit vs file_get_contents().",
        "Do not use mmap for append-heavy workloads — extending a mapping requires re-mapping, which is expensive."
    ],
    "related": [
        "opcache",
        "php_execution_model",
        "php_fpm"
    ],
    "prerequisites": [],
    "refs": [
        "https://linux.die.net/man/2/mmap",
        "https://www.php.net/manual/en/opcache.configuration.php"
    ],
    "bad_code": "; opcache too small — workers silently recompile scripts on every request:\nopcache.memory_consumption = 64   ; MB — too small for a 500-file app\nopcache.max_accelerated_files = 2000",
    "good_code": "; Size to fit entire compiled codebase + headroom:\n; Check current usage: opcache_get_status()['memory_usage']\nopcache.memory_consumption = 256  ; MB\nopcache.max_accelerated_files = 10000\nopcache.validate_timestamps = 0   ; production: disable stat() per request",
    "example_note": "The bad opcache config is too small — once full, PHP silently falls back to re-compiling scripts on each request, negating the cache entirely. The good config sizes memory to the codebase and disables per-request timestamp checking in production.",
    "created": "2026-03-31",
    "updated": "2026-03-31",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/memory_mapped_files",
        "html_url": "https://codeclaritylab.com/glossary/memory_mapped_files",
        "json_url": "https://codeclaritylab.com/glossary/memory_mapped_files.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Memory-Mapped Files](https://codeclaritylab.com/glossary/memory_mapped_files) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/memory_mapped_files"
            }
        }
    }
}