memshare: Fast Shared-Memory Parallelism in R

Why memshare?

Most parallel R workflows duplicate large objects into every worker process. That wastes RAM and time. memshare stores big objects once in shared memory and lets workers attach to them as ordinary R vectors/matrices via ALTREP views. You get:

This vignette is a quick, practical guide, for technical details we refer to [Thrun and Märte, 2025]


Install

install.packages("memshare")         # CRAN
# remotes::install_github("yourname/memshare")  # dev

Requirements: R ≥ 4.0, C++17 toolchain.


5‑minute tour

1) Column-wise work on a matrix (memApply)

library(memshare)

set.seed(1)
n <- 10000; p <- 2000
X <- matrix(rnorm(n * p), n, p)   # numeric/double matrix
y <- rnorm(n)

# Correlate each column with y, in parallel, without copying X to workers
res <- memApply(
  X = X, MARGIN = 2,
  FUN = function(v, y) cor(v, y),
  VARS = list(y = y)           # shared side data
)
str(res)

What happened?
X and y were placed in shared memory; workers received views (ALTREP) instead of copies. Each worker extracted the i-th column as v, ran FUN(v, y), and returned a result. All views were released automatically at the end.

2) List workloads (memLapply)

list_length <- 1000
d <- 200
L <- lapply(1:list_length, function(i) matrix(rnorm(d * d), d, d))
w <- rnorm(d)

ans <- memLapply(L, function(el, w) el %*% w, VARS = list(w = w))
length(ans); dim(ans[[1]])

3) Low-level control (register / retrieve / release)

ns <- "demo"
X  <- matrix(rnorm(1e6), 1000, 1000)
registerVariables(ns, list(X = X))

vw <- retrieveViews(ns, "X")
mean(vw$X[ , 1])
releaseViews(ns, "X")

releaseVariables(ns, "X")

Concepts that matter

Unload the package (or release views/variables) to clean up. Memory is freed once no views remain.


Common patterns

Feature map over columns (fast and memory-light)

score <- function(v, a, b) sum((v - a)^2) / (1 + b)  # any column-wise work
ns <- "scores"
a <- rnorm(n); b <- runif(1)

out <- memApply(X = X, MARGIN = 2, FUN = score, VARS = list(a = a, b = b), NAMESPACE = ns)

Multiple passes on the same data

Reuse the same namespace to avoid re-registering large objects.

ns <- "reuse"
registerVariables(ns, list(X = X))
pass1 <- memApply("X", 2, function(v) sd(v), NAMESPACE = ns)
pass2 <- memApply("X", 2, function(v) mean(v), NAMESPACE = ns)
releaseVariables(ns, "X")

Tips and best practices


Troubleshooting


Essentials


References

[Thrun and Märte, 2025] Thrun, M.C., Märte, J.: Memshare: Memory Sharing for Multicore Computation in R with an Application to Feature Selection by Mutual Information using PDE, The R Journal, in revision, 2025.



Try the memshare package in your browser

Any scripts or data that you put into this service are public.

memshare documentation built on Dec. 5, 2025, 9:07 a.m.