Memory-Mapped Files (mmap) Interview Preparation Guide

🧠

Ready to test yourself?

Each test is 5 questions with varying difficulty.

Master AI/ML with AI Prep app

AI Prep covers AI Agents, Generative AI, ML Fundamentals, NLP & LLMs and a lot more, with adaptive tests and daily challenges. Fully offline on Android. Free to try, one-time unlock for lifetime access.

Download AI Prep, Free to Try

Introduction

Memory-Mapped Files (mmap) represent a fundamental technique in systems programming that allows a process to map a file directly into its virtual address space. By treating file contents as an array in memory, developers can bypass traditional read/write system calls, significantly reducing context switches and data copying. In 2026, as high-throughput AI inference engines and low-latency databases demand maximum I/O efficiency, mmap has become a critical skill for performance-oriented engineers. Roles such as Systems Engineer, Database Developer, and Kernel Engineer frequently require deep knowledge of how mmap interacts with the OS page cache. Interviewers test this topic to gauge your understanding of virtual memory, kernel-level I/O, and the trade-offs between memory-mapped access and standard buffered I/O. Junior candidates are expected to understand the basic API and the concept of demand paging, while senior candidates must demonstrate mastery over page faults, memory consistency, and the architectural implications of mapping large files on multi-threaded systems.

Why It Matters

The primary value of mmap lies in its ability to eliminate the 'double buffering' problem, where data is copied from the kernel page cache into user-space buffers. By mapping a file, the process accesses the page cache directly, enabling zero-copy I/O. This is critical for high-performance systems like RocksDB or Vector Databases, where reading large datasets from disk into memory must be near-instantaneous. In 2026, with the rise of massive model weights and large-scale vector indices, the ability to map gigabytes of data without exhausting physical RAMβ€”relying on the OS to swap pages in and out via demand pagingβ€”is a standard architectural pattern. This topic is a high-signal interview subject because it forces a candidate to explain how their code interacts with the operating system kernel. A strong answer reveals an understanding of page tables, hardware interrupts (page faults), and the cost of context switching. A weak answer often ignores the complexities of page cache contention or the risks of SIGBUS errors when a file is truncated while mapped, signaling a lack of production-grade systems experience.

Core Concepts

Architecture Overview

The mmap architecture relies on the interaction between the process's virtual address space, the kernel page tables, and the filesystem's page cache. When a file is mapped, the kernel creates a virtual memory area (VMA) for the process. No physical pages are allocated initially. When the process accesses an address within this range, the CPU triggers a page fault. The kernel's page fault handler then checks the page cache; if the data is present, it maps the physical frame to the process's page table. If not, it initiates a disk read to populate the page cache.

Data Flow
  1. Process
  2. Page Table
  3. Page Cache
  4. Disk
  [Process Virtual Memory] 
             ↓ 
      [Page Fault Handler] 
             ↓ 
    [Kernel Page Table] 
      ↓            ↓ 
[Page Cache Hit] [Page Cache Miss] 
      ↓            ↓ 
[Return Physical] [Disk Controller] 
      ↓            ↓ 
[CPU Access Data] [Load to Cache]
Key Components
Tools & Frameworks

Design Patterns

Memory-Mapped Ring Buffer Communication

Using a shared mmap region between two processes to implement a lock-free circular queue.

Trade-offs: High throughput but requires complex synchronization (atomic operations).

Lazy File Loading Performance

Mapping a massive file and relying on demand paging to load only accessed segments.

Trade-offs: Reduces startup time but introduces latency during initial access.

Write-Back Batching Persistence

Modifying mapped memory and using msync periodically to flush changes.

Trade-offs: Improves write performance but risks data loss on crash.

Common Mistakes

Production Considerations

Reliability Handle SIGBUS signals to prevent crashes when files are modified externally.
Scalability Use large mappings to reduce page table entries and kernel overhead.
Performance Use madvise(MADV_SEQUENTIAL) for streaming reads and madvise(MADV_RANDOM) for index lookups.
Cost Reduces CPU usage by offloading data movement to the kernel's DMA-backed page cache.
Security Use PROT_READ only where possible to prevent accidental memory corruption.
Monitoring Track major/minor page faults via /proc/[pid]/stat.
Key Trade-offs
β€’Latency vs Throughput
β€’Memory usage vs Disk I/O
β€’Complexity vs Performance
Scaling Strategies
β€’Large-page (HugeTLB) support
β€’Mapping file segments
β€’Pre-faulting pages
Optimisation Tips
β€’Use MAP_POPULATE to pre-fault pages
β€’Align mappings to page boundaries
β€’Use madvise to guide kernel prefetching

FAQ

Is mmap always faster than read()?

Not necessarily. mmap is faster for large, random-access files where you want to avoid copying. For small files or sequential reads, the overhead of page table management and page faults can make read() more efficient.

What happens if a file is truncated while mapped?

If a process accesses a memory address that no longer corresponds to a valid file offset, the kernel sends a SIGBUS signal to the process, which usually results in a crash unless specifically handled.

What is the difference between MAP_SHARED and MAP_PRIVATE?

MAP_SHARED reflects changes to the underlying file and makes them visible to other processes. MAP_PRIVATE uses copy-on-write semantics, meaning changes are local to the process and never written to the file.

How does mmap interact with the page cache?

mmap maps the kernel's page cache directly into the process's virtual address space. This allows the process to read and write data as if it were in local memory, bypassing the need for explicit read/write system calls.

Can I map a file larger than my physical RAM?

Yes. The OS uses demand paging to load only the necessary parts of the file into physical RAM. Pages are evicted when memory pressure occurs, allowing you to work with files much larger than available RAM.

What is a major vs. minor page fault?

A minor page fault occurs when the page is in the page cache but not in the process's page table. A major page fault occurs when the page must be fetched from the disk, causing significant latency.

How do I ensure data written to mmap is saved to disk?

You must call the msync() syscall, which forces the kernel to flush the dirty pages in the page cache to the underlying storage device, ensuring durability.

What is the role of madvise()?

madvise() allows you to provide hints to the kernel about how you intend to use a memory region (e.g., sequential vs. random access), which helps the kernel optimize its readahead and eviction policies.

Does mmap work on network filesystems?

It can, but performance is often poor due to network latency and cache consistency issues. Most network filesystems (like NFS) have specific limitations regarding mmap behavior.

What is the limit on the number of mappings?

The limit is defined by the kernel parameter vm.max_map_count. Exceeding this limit will cause mmap calls to fail, which is a common issue in applications that create many small mappings.

Related Roles

Master AI/ML with AI Prep app

AI Prep covers AI Agents, Generative AI, ML Fundamentals, NLP & LLMs and a lot more, with adaptive tests and daily challenges. Fully offline on Android. Free to try, one-time unlock for lifetime access.

Download AI Prep, Free to Try
← Back to Interview Prep