Start from a Symptom

STATUS: DEVELOPINGLEVEL: INTROREVIEWED: 2026-11-18

Something has gone wrong in your own R code, and you can see what but not why. Each section below names a common symptom, gives the usual mechanism in a few sentences, and links to the pages that explain it. For an ordered route instead, follow one of the reading paths.

More cores made the job slower

Every worker costs something before it does any work. A PSOCK worker is a fresh R process that has to start and load its packages, and every task and every result is serialized to cross a socket. When each task is short, that fixed cost can be larger than the compute a worker takes over, so each added worker adds more overhead than it removes. Workers also compete for memory bandwidth and cache, and a worker that calls a multithreaded BLAS starts threads of its own, until the machine has more runnable threads than cores. Give each worker larger chunks of work, try fewer workers than you have cores, and cap the native threads in each worker.

Read:

My workers use far more memory than I expected

A forked worker shares the parent’s memory pages copy-on-write: a page stays shared until one side writes to it, and then the kernel gives the writer a private copy. R’s garbage collector writes into the objects it scans. A full collection clears and sets the mark bit in the header of every reachable object, so the pages that hold those objects stop being shared, and each worker ends up paying for its own copy. My sunlight benchmarks found fork saving far less memory than the shared pages suggest; the experiment record has the numbers and their caveats. Measurement can mislead too: summing RSS across workers counts every shared page once per worker, so the total looks larger than the memory in use.

Read:

The result changes when I change the worker count

parallel::clusterSetRNGStream() gives each worker its own L’Ecuyer-CMRG stream, so the random numbers a task draws depend on which worker runs it and how many tasks that worker ran before. Change the worker count and tasks land on different workers, so the results move. Results that do not depend on the worker count need one stream per task, all derived from one seed. future.apply does this when you pass future.seed, and sunlight keys a stream to each task. Calling set.seed(i) at the start of task i also makes results independent of the worker count, but L’Ecuyer-CMRG streams are built not to overlap and nearby seeds carry no such promise.

Read:

This works on Linux and fails on Windows

parallel::mclapply() forks the R session, and Windows has no fork. On Windows it runs on one core, and asking for more stops with 'mc.cores' > 1 is not supported on Windows. A forked child starts with a copy-on-write view of everything the parent had: loaded packages, data, options. A PSOCK cluster, which makeCluster() creates by default on every platform, starts fresh R processes that have none of it. Code that worked under fork then fails with “object not found” or “could not find function” until the objects are exported and the packages loaded on each worker. Testing only with fork on Linux hides those dependencies on the parent session.

Read:

A large object gets copied to every worker

R serializes a function together with its enclosing environment, unless that environment is the global environment, the base environment, an attached package environment or a package namespace. A task function created inside another function carries every object in that function’s frame, used or not, so a data frame of a million rows sitting next to it travels too. parLapply() sends the function with each chunk, once per worker by default, and clusterApplyLB() sends it with every task. Create task functions at top level, pass what they need as arguments, and send a large shared object once per worker with clusterExport(). Under fork, an object created before the fork is already in every worker, shared copy-on-write.

Read:

A package works sequentially and fails inside workers

Three mechanisms cause most of these failures. An external pointer, such as a database connection, an Rcpp XPtr or a model held by a C++ library, does not survive serialization: the worker receives a pointer set to NULL, and the first call through it fails. A PSOCK worker has not loaded the parent’s packages, so each package must be loaded inside the worker, where its initialization code runs again. Under fork, the child gets only the thread that called fork(). If another thread of a native library held a mutex at that moment, the child’s copy of the mutex stays locked with no thread left to release it, and the first call that needs it hangs. Open connections inside each worker, and do not fork a session after a package has started threads of its own.

Read:

R is using more threads than I asked for

The worker count sets how many R processes run. Native libraries start threads of their own: OpenBLAS and Intel MKL run large matrix operations on several threads, and packages compiled with OpenMP, data.table among them, start their own thread teams. R’s bundled reference BLAS is single-threaded, so the BLAS part applies when R is linked to an optimized one. Two workers that each start four BLAS threads put eight threads on the machine, and on four cores those threads take turns instead of running at once. Cap the threads inside each worker. OpenBLAS reads OPENBLAS_NUM_THREADS or OMP_NUM_THREADS when it starts, so set them in the environment the workers start with; RhpcBLASctl changes the BLAS thread count at run time, and data.table::setDTthreads() sets data.table’s. Then count the threads with system tools, because the worker count says nothing about them.

Read:

If none of these fit

The cause may be one of the constraints built into R itself. Why R Is Difficult to Parallelize traces the shared root causes, and the glossary defines the terms used across the corpus.

Where to go next

Back to top