Fundamentals 11 min read

How mmap Lets You Treat Disk Files Like Memory – Benefits and Pitfalls

mmap maps a file directly into a process’s address space, allowing programmers to read and write disk data as if it were memory, eliminating extra system calls and copies, while introducing complexities like page faults, address‑space limits, and performance trade‑offs that must be evaluated per workload.

IT Services Circle
IT Services Circle
IT Services Circle
How mmap Lets You Treat Disk Files Like Memory – Benefits and Pitfalls

This article explains what mmap is, how it works, and when it is advantageous to use it.

Simple vs. Complicated

Reading and writing memory in code is trivial, e.g.:

int a[100];
a[0] = 2;

Reading and writing a disk file requires opening the file, obtaining a file descriptor, and then performing read/write calls, which is more cumbersome:

char buf[1024];
int fd = open("/filepath/abc.txt");
read(fd, buf, 1024); // operate on buf

The difficulty stems from the different addressing granularity: memory is byte‑addressable, while disk files are addressed in blocks, requiring the OS to copy data into memory before byte‑level access.

Why It Is Tricky

Disk access involves a layer of virtual memory. The OS presents a continuous address space to the process, but the underlying file resides on disk in block units. The OS must translate block accesses into byte‑level operations for the program.

Mapping Files Directly with mmap

Using virtual memory, a file can be mapped into a process’s address space so that reading or writing the mapped region directly manipulates the file:

// Example: map 100‑byte file to addresses 600‑800
// After mapping, accesses to 600‑800 affect the file bytes.

When the process first accesses the mapped region, the OS loads the corresponding disk pages (handling page faults). Subsequent reads/writes operate on memory, and the OS writes back changes to disk automatically.

Performance Considerations

Standard read/write involves system calls and copies between kernel and user space, incurring overhead. mmap avoids these copies, but introduces its own costs: kernel data structures for the mapping and page‑fault handling. Whether mmap outperforms standard I/O depends on the relative cost of system calls versus page faults on the target platform.

Large‑File Handling

For files larger than physical RAM, traditional I/O requires explicit buffering. mmap can map the whole file (or large portions) into the process address space; the OS swaps out rarely used pages, allowing the program to work with files larger than available memory transparently.

On 32‑bit systems the address space may be insufficient for very large mappings, while 64‑bit systems generally have ample space.

Memory Savings with Shared Libraries

Dynamic libraries are loaded via mmap. Multiple processes can map the same library into their address spaces, but the library’s pages reside only once in physical memory, saving both disk and RAM compared to static linking.

When a process accesses the library code, the OS loads the needed pages on demand, sharing them across processes.

When to Use mmap

Advantages include:

Eliminating extra system‑call and copy overhead.

Simplifying code for large‑file processing.

Sharing read‑only data (e.g., dynamic libraries) across processes.

Drawbacks include potential page‑fault overhead, mapping limits on 32‑bit systems, and the need to benchmark for the specific workload.

图片
图片
图片
图片
图片
图片
图片
图片
图片
图片
图片
图片
图片
图片
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

mmapVirtual MemoryFile I/Omemory mapping
IT Services Circle
Written by

IT Services Circle

Delivering cutting-edge internet insights and practical learning resources. We're a passionate and principled IT media platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.