Linux · C/C++ · Memory
mmap, Page Cache and Page Faults: How Linux Really Brings Files into Memory
A file is not simply “loaded into RAM”. Linux turns the process into a small systems lecture.
With large PDFs, image data and OCR pipelines, it is worth looking below the abstraction layer.
mmap() can map file data directly into a process' virtual address space, but physical memory pages still become relevant only when they are actually accessed.
Virtual memory first, real pages later
A process works with virtual addresses. The kernel and the MMU make sure those addresses are mapped to physical memory pages or other backing stores. With a file-backed mmap(), Linux first reserves a suitable range in the process virtual address space.
That does not mean the entire file is copied into RAM immediately. When the process later accesses a page that is not mapped yet, a page fault occurs. The kernel then establishes the mapping or first has to obtain the required data from storage.
The page cache sits in the middle
Regular file data on Linux is typically cached through the page cache. As a result, read() and mmap() can refer to the same cached file contents. That matters: mmap() is not a secret shortcut around the operating system.
If a required file page is already in the page cache, access can be relatively cheap. If it first has to be read from storage, the operation becomes much more expensive. That is why a second pass over the same file can appear dramatically faster even though your algorithm has not changed at all.
Minor and major page faults
Not every page fault means disk I/O. In simplified terms, two useful cases can be distinguished:
| Type | What happens? | Cost |
|---|---|---|
| Minor Fault | The page is already available but still needs to be entered into the process page tables. | Relatively cheap, with no required storage access. |
| Major Fault | The required data is not resident in memory yet and first has to be read in. | Much more expensive because I/O may be involved. |
The exact internal situation can be more complex, but this distinction already explains why two apparently identical memory accesses can have completely different timing.
A small mmap example
For read-only access, a file can for example be mapped like this:
int fd = open(path, O_RDONLY);
struct stat st;
fstat(fd, &st);
const unsigned char *data = mmap(
NULL,
(size_t)st.st_size,
PROT_READ,
MAP_PRIVATE,
fd,
0
);
if (data == MAP_FAILED) {
/* handle error */
}
/* Pages are needed only when actually accessed. */
unsigned char first = data[0];
munmap((void *)data, (size_t)st.st_size);
close(fd);
Real production code intentionally needs more edge-case handling here, including empty files and checking every system call. The example is meant to show the mechanism, not to create a fresh reason for a service to crash on a Sunday evening.
Why mmap is not automatically faster
mmap() can reduce copying and bookkeeping overhead and is particularly convenient when data should be accessed like an array. Even so, it does not automatically beat read() or pread().
- For strictly sequential processing,
read()with a sensible buffer can be highly efficient. - Many random accesses across a huge mapping can trigger large numbers of page faults.
- Access patterns determine whether readahead and caching work well.
- Parallel processing additionally brings page tables, NUMA effects and memory bandwidth into the picture.
The useful question is therefore not “Is mmap faster?” but “Does this access pattern fit mmap, and have I measured it?” Hardware still stubbornly refuses to optimize architecture decisions based on intuition.
What this means for OCR and document processing
Document pipelines combine large amounts of data with many processing stages: PDF parsing, rendering, image preprocessing, OCR and export. If large intermediate results are copied repeatedly or the same file ranges are constantly reread, memory traffic can grow faster than the actual computation.
Memory mapping can be useful for large, immutable data sets, such as index files or binary page buffers. For rendered images that must be transformed anyway, a conventional owned buffer is often simpler and at least as reasonable.
mmap() maps a file into virtual address space. The page cache keeps file data in memory. Page faults make required pages actually accessible. These are three different mechanisms, even though they can all meet at a single memory access.
Conclusion
Anyone processing large documents for performance should look beyond CPU time. Virtual memory, the page cache and access patterns all influence how much real work the system must do while reading data.
mmap() is a tool, not a magic trick. Its strength appears when the data structure and access pattern actually fit it.