10.2 Memory Mapping

IPCSTATUS: DEVELOPINGLEVEL: INTERMEDIATEREVIEWED: 2026-11-18
Before this page Serialization

10.2.1 Motivation

Moving data between processes is expensive: serialization and deserialization both cost time, and a copy from one address space to another goes through the kernel. Memory mapping takes a different approach and lets two processes see the same physical memory without copying it. What they share is less than what threads share. The same bytes can sit at different addresses in each process, so pointers stored in them do not carry over, and a garbage collector in one process knows nothing about readers in the other.

10.2.2 The OS mechanism

Memory mapping is a virtual memory feature. The operating system maintains a mapping from virtual addresses (what the program sees) to physical memory pages (actual RAM). Multiple processes can map the same physical page into their own virtual address space at different virtual addresses.

Process A: virtual address 0x10000 -> physical page 0x40000
Process B: virtual address 0x20000 -> physical page 0x40000

// Both processes see the same physical page
// But at different virtual addresses

When a process reads at virtual address 0x10000, the CPU’s memory management unit translates it to physical address 0x40000. Another process reading at virtual address 0x20000 also translates to physical address 0x40000. Both see the same data.

File-backed mappings

A process can map a file into its address space.

file_handle = open("/data/large.bin")
mapping = mmap(NULL, 1000000, PROT_READ | PROT_WRITE,
               MAP_SHARED, file_handle, 0)
// mapping is a pointer to 1 MB of data from the file

The OS reads file pages on demand. The first access to each page of the mapping causes a page fault. The OS loads that page from disk into RAM. Subsequent reads use the cached page.

If another process maps the same file at a different virtual address, both processes see the same physical pages (if the pages are still cached). This saves memory compared to reading the file twice.

Anonymous mappings

A process can allocate anonymous memory (not backed by a file) and share it with another process.

// Parent process
mapping = mmap(NULL, 10000000, PROT_READ | PROT_WRITE,
               MAP_SHARED | MAP_ANONYMOUS, -1, 0)
// Anonymous, shared mapping, 10 MB

child_pid = fork()
if child_pid == 0:
  // Child process
  // mapping is visible at the same virtual address
  // Both parent and child write to it

After fork, the child inherits the mapping at the same virtual address. Because it is MAP_SHARED, both processes see the same 10 MB, and a write by either one is visible to the other; no copy is made. Private memory behaves differently after fork. A MAP_PRIVATE mapping, like the ordinary heap, is shared copy-on-write: the first write to a page makes the kernel copy it, and from then on each process has its own version.

10.2.3 Limits of mapping R objects

The obstacle is that R objects are pointer graphs with garbage collection metadata.

A serialized R vector is a byte sequence: [type, length, data]. Deserializing creates a new SEXP pointing to a new heap allocation.

An R vector in memory is a header followed by its data, and the header holds pointers: to the attributes, and to the neighboring nodes in the collector’s lists. A list’s data are pointers to other SEXPs. The entire graph lives in the heap.

Suppose two processes map the same physical memory containing R objects, each at a different virtual address.

Process A: SEXP at virtual address 0x7fff0000
           points to data at virtual address 0x7fff1000

Process B: same SEXP at virtual address 0x6000c000  // Different!
           points to... what?
           // The pointer 0x7fff1000 is invalid in Process B
           // It is a virtual address from Process A

A pointer stored inside the object is meaningless in Process B. Dereferencing it crashes or accesses random memory.

An R object is a pointer graph with mutable GC metadata in every header: a mark bit, a generation and a reference count. Mapping memory does not transport pointers correctly. Each process must have its own pointer graph, or the objects must be immutable and pointer-free (just bytes).

10.2.4 Shared memory that works: immutable byte sequences

Memory mapping works well for immutable data: arrays of bytes, numeric matrices, and character strings that are never modified.

An 800 MB matrix can be stored in a file. Multiple R processes map the file. All see the same physical memory. No copy. The matrix is read-only.

# Pseudocode
matrix_data <- mmap_file("matrix.bin", nrow=10000, ncol=10000, readonly=TRUE)
# matrix_data is a numeric matrix in shared memory
# Pages load from disk on first touch
# Modifying it is forbidden (or copies the modified page)

A standard R vector keeps its data inline, right after its header, so GNU R cannot point an ordinary vector at mapped memory. Packages work around that in two ways. bigmemory and mmap give R an external pointer to the mapping and read it through their own accessor functions. An ALTREP class can return the mapped address as a vector’s data pointer, so the mapped data looks like an ordinary R vector; mori does this over POSIX shared memory or a Windows file mapping. Either way the object R manages is small and local to each process, while the gigabytes behind it are shared.

10.2.5 Copy-on-write fork

A special case of mapping: fork inherits all virtual mappings.

// Parent process allocates memory
x <- c(1, 2, 3)

child_pid = fork()
if child_pid == 0:
  // Child process
  // Child sees x at the same address as parent
  // But the page is marked copy-on-write
  // If child writes to x, the OS copies the page first
else:
  // Parent process
  // If parent writes to x, the OS copies the page first

Copy-on-write lets parent and child share memory at first and diverge page by page as either one writes. sunlight relies on this to start workers quickly. In R the sharing does not last, because a full collection in the child writes to the header of every object it inherited. fork, copy-on-write, and the first GC measures how little sharing survives that.

In sunlight’s Day 7 run of 2026-08-01, the summed PSS of its forked workers was only 1.28x below the summed RSS of the best incumbent framework at 8 workers and 1.10x below at 32. Setting PSS against RSS favors sunlight; the run was on WSL2, and crew is left out because it did not start all its workers. The cause is R’s collector. A full collection walks every node in the generations it collects, reachable or not, clearing the mark bit in each header and then setting it again in each reachable one (src/main/memory.c), so every page that holds a node header becomes private to the child. The data pages of a large vector can stay shared, since only the header at the start of its block is written.

10.2.6 Use cases for memory mapping in parallel R

Large immutable datasets: A data frame stored as an uncompressed Arrow IPC (Feather) file. Multiple R processes map it and read the same physical pages, with no deserialization. Parquet does not work this way: its pages are encoded and usually compressed, so a reader has to decode them into new memory.

Temporary work pools: A work-stealing scheduler’s task queue can live in shared memory. Workers map it and steal tasks. No serialization of task metadata.

Logging and telemetry: A shared memory ring buffer logs events. Multiple processes write and read without serialization.

Constraints: Mapped memory is process-isolated at the virtual address level. Pointers are not portable. The data must be immutable or mutable via specific synchronization (locks on shared data). Garbage collection of shared data is complex. If one process deletes an object from shared memory while another uses it, crashes follow.

10.2.7 Alternatives

Serialization: Convert objects to bytes. Slower but portable and safe. Every object is copied.

Interpreters in one process: Several R interpreters share one address space, so a pointer means the same thing in each of them. Each interpreter can still own its heap, as in threaded-r, and then values cross between interpreters as copies; threaded-r’s channels serialize them. Interpreters sharing one heap would need synchronization in place of serialization.

Read-only code sharing: Preload functions and packages in a template process. Fork workers. Code is shared via copy-on-write until the child’s first full collection privatizes most of it. Data is sent by value.

10.2.8 Comparison

Approach Copy Cost Pointer Safety Serialization Best for
Memory mapping None (for read-only) Unsafe (pointer portability) None Immutable bulk data
Serialization Full (2x) Safe Yes, slow Small objects, any type
Fork copy-on-write Every page either process writes (in R, most of the heap after a full GC) Safe (same address) No Preloaded templates
Shared memory, shared heap None Unsafe (GC coordination) No, locks instead Threads sharing one heap

flowchart LR
  pa["process A"] -->|mmap| va["virtual address A<br/>(A's page table)"]
  pb["process B"] -->|mmap| vb["virtual address B<br/>(B's page table)"]
  va --> page["one physical page<br/>in RAM"]
  vb --> page
  page -->|backed by| file[("file or shared<br/>memory object")]

Memory mapping: two processes see the same physical page at different virtual addresses. Pointers are address-dependent and not portable.

Further reading

Where this appears

  • sunlight sunlight’s template process forks the workers, so they start out sharing its pages copy-on-write. R’s collector privatizes most of those pages once a worker runs R code.
  • r-in-rust (rrs) rrs could use mmap for large immutable data structures to avoid repeated serialization.

Where to go next

Back to top