2.4 Measuring Memory Correctly

MEASUREMENTSTATUS: DEVELOPINGLEVEL: INTERMEDIATEREVIEWED: 2026-11-15

Memory reporting is full of traps. The number shown by a monitoring tool is often not the number you think it is.

2.4.1 Resident set size versus virtual size

A process has two memory sizes. Virtual size is the total address space the process can access. Resident set size (RSS) is the physical memory in use right now.

Virtual size includes memory that is allocated but not yet touched. If you allocate a gigabyte array and never write to it, virtual size increases by a gigabyte. Physical memory does not. Tools such as ps report virtual size as VSZ, and it is usually far larger than the memory in use. R’s own gc() reports neither number: its table counts the cons cells (Ncells) and vector cells (Vcells) of R’s heap, as used, trigger and maximum, in megabytes.

Resident set size is more meaningful, since it counts pages that are in RAM. But RSS has a hidden complexity: shared pages.

2.4.2 Shared and private pages

When a process forks, the child starts with the same address space as the parent. Both processes point to the same physical memory pages. When the child writes to a page, the operating system makes a copy. That page becomes private to the child. The parent keeps its own copy. This is copy-on-write.

The kernel tracks which pages are shared and which are private. Shared pages show up in the RSS of both parent and child.

The double-counting trap

If you sum the RSS of the parent and child, you count each shared page twice, once in each process’s RSS, while only one copy exists in physical memory.

This is where memory measurement goes wrong. A careless observer sums RSS across all workers and concludes that fork saved little or nothing. The sum counts every shared page once per process that maps it, so it overstates the physical memory in use and hides the saving.

2.4.3 Proportional set size solves the counting problem

A more accurate metric is proportional set size (PSS). PSS divides shared pages equally among all processes that share them. If a page is shared by four processes, each process gets 0.25 of that page in its PSS. Summing PSS across all processes gives the true total memory used. A third number, unique set size (USS), counts only the pages private to one process, which is what that process would give back if it exited.

If memory measurement tools reported PSS instead of RSS, fork memory results would be clearer and less deceptive.

Some tools report PSS. Linux provides it in /proc/[pid]/smaps, along with Private_Clean and Private_Dirty, whose sum is the USS. On macOS, memory measurement is harder because the kernel does not export PSS directly.

2.4.4 Worked example of the trap

Parent process has RSS of 1 GB total: 800 MB shared with children, 200 MB private.

Child process 1 has RSS of 900 MB: 800 MB shared with parent and other children, 100 MB private.

Child process 2 has RSS of 900 MB: 800 MB shared with parent and other children, 100 MB private.

If you sum RSS: 1000 + 900 + 900 = 2800 MB.

The truth: 800 MB shared, counted once, plus 200 + 100 + 100 = 400 MB private, is 1200 MB total.

The sum overcounted by 1600 MB: the 800 MB of shared pages was counted three times instead of once.

2.4.5 Forked processes

My work on sunlight measures fork-based parallelization, and one of its questions was whether forking saves memory for R workloads.

sunlight’s kill criterion asked for aggregate worker memory at least 5x below the best incumbent framework. The Day 7 benchmarks of 2026-08-01 measured 1.28x at 8 workers and 1.10x at 32, comparing the summed PSS of sunlight’s workers against the summed RSS of the incumbents’ workers. The incumbents start fresh processes that do not share heap pages, which is why the project counted their RSS directly; those processes still share mapped shared-library pages, so the comparison leans somewhat toward sunlight. The host was WSL2, and crew is left out because it did not start all its workers.

Summing RSS would have made sunlight look worse still. Picture eight forked workers that each show 1 GB of RSS, of which 250 MB is private and the rest is shared with the template. The RSS sum says 8 GB. The physical total is the 750 MB of shared pages, counted once, plus 2 GB private: under 3 GB. A reader who sees only the RSS sum concludes that fork saved nothing.

2.4.6 The first garbage collection writes to every page

In sunlight’s measurements, a forked worker shared nearly all of the template’s memory until it ran R code, and only about a fifth of it afterwards: the pages the collector never writes, such as mapped shared libraries. A plain-R check with no sunlight code showed the same jump after a single gc() in the child. R’s full garbage collection clears the mark bit in the header of every node and then sets it again in every reachable one, relinking each reached node between lists as it goes (src/main/memory.c), and each of those writes privatizes the page it lands on. That is why the aggregate figures above stayed far short of the 5x goal.

Step by step

  1. Child process forks. It shares nearly all pages with the parent.
  2. Child starts running a task. Memory is mostly shared at this point.
  3. Child triggers a garbage collection. A full collection first clears the mark bit of every node in the heap, then walks the reachable object graph and sets it again.
  4. Each of those is a write to an object header.
  5. Each mark bit write dirties a page. The kernel detects the write and copies the page from the parent.
  6. After collection, nearly every page of R objects is private to the child.

The saving ends at that first full collection. In sunlight’s measurements the heap went private during worker start-up, before any task ran, though the notes do not record which collection level ran then. From then on a worker uses about as much memory as a freshly started one.

Consequences

The problem is not fork itself but a collector that writes into the objects it marks. R keeps its mark bits in the object headers and links every object into a generation list, and marking writes both. Every such write lands on a shared page and makes the kernel copy it.

Other runtimes have two ways out, and R has neither today:

  1. Keep mark state outside the objects, in a separate bitmap, so marking writes only to the bitmap’s pages. Ruby moved to bitmap marking in 2.0 for this reason. In R the generation lists would have to move out too, because marking also relinks every object it reaches.
  2. Keep inherited objects out of the collector’s reach. Python’s gc.freeze(), added in 3.7, moves every tracked object into a permanent generation that later collections ignore, for exactly this fork pattern.

Without either, the copy-on-write saving lasts only until the first full collection.

2.4.7 Allocator retention

Another hidden factor is allocator behavior. Allocators often keep freed memory for reuse instead of returning it to the operating system. A program that frees gigabytes of objects can still show high RSS because the allocator has not released the pages back to the OS. The pages are still marked as private to this process.

To see what went back to the operating system, force a collection with gc() and read RSS right after it. Do not assume that memory a program says it freed has returned to the operating system.

2.4.8 Correct measurement of fork memory

If you measure fork-based parallelization, report PSS or take individual snapshots before and after tasks. Do not sum RSS across workers. If you must sum it, say plainly that the total overcounts shared memory, and give the physical figure alongside.

Better practice reports four numbers: parent RSS, child RSS before the first task, child RSS after it, and memory returned to the operating system after cleanup. Track them across several children and the real floor appears.

The sunlight measurement

I measured the same workers at four points: right after fork, after the worker’s start-up, after a trivial task, and after a gc() in the worker. Measuring by stage is what separated the copy-on-write window from what follows it. The result was not that fork saves memory, but that the saving has a lifetime: it lasts until the child’s collector writes into the inherited object headers, and after that the floor is set by garbage collection design, not by anything fork did. The window closed during start-up, before the first task, so a single end-of-run number would have hidden it completely.

2.4.9 Amdahl’s law and memory

Just as Amdahl’s law sets a ceiling for speedup, memory usage has a floor: the shared pages counted once, plus every page each process has made private. Copy-on-write keeps the second term small only while the pages stay shared.

Take eight workers forked from a 1 GB template, each allocating 200 MB of its own data. Before any collection the total is about 1 + (8 × 0.2) = 2.6 GB. Once each worker’s first full collection has privatized the heap pages it inherited, each worker also holds its own copy of those, and the total heads toward 1 + 8 × (1 + 0.2) = 10.6 GB, less whatever the collector never writes, such as shared libraries and the data pages of large vectors.

R’s collector sets that second figure, not fork, so tuning the fork does not move it.

2.4.10 When RSS is the right metric

RSS is not useless. For a single process, RSS is a fair first number, though it still counts shared-library pages that other processes use too. For forked processes, RSS is misleading without explanation. For multi-threading shared-memory systems (where all threads share all pages), RSS is accurate because there is no sharing question.

RSS is also useful for detecting memory leaks. If a process’s RSS keeps growing under a steady workload, suspect a leak, or allocator fragmentation. RSS is the right metric for that question.

The key is matching the metric to the question. Asking “does fork save memory?” and answering with summed RSS is the wrong match. The match breaks down in the presence of copy-on-write.

Further reading

Where this appears

  • sunlight Fork copy-on-write memory measurement and the double-counting trap are central to interpreting fork performance.

Where to go next

Back to top