memshare enables multicore computation in R withoutredundant memory copies. Large vectors, matrices, or lists are storedonce in shared memory and exposed to R processes asALTREPviews. This allows workers in a PSOCK cluster to operate on thesame physical data while avoiding serialization overhead.
Key features:
shm_openon Unix,MapViewOfFile on Windows).parallel::parApply andparallel::parLapply:memApply() — apply a function row/column-wise over amatrix in shared memory.memLapply() — apply a function element-wise over a listin shared memory.From CRAN:
install.packages("memshare")From GitHub (development version):
remotes::install_github("mthrun/memshare")System requirements: R ≥ 4.0 and a C++17 compiler.
library(memshare)library(parallel)set.seed(1)n=10000p=2000X=matrix(rnorm(n* p), n, p)y=rnorm(n)res=memApply(X = X,MARGIN =2,FUN =function(v, y)cor(v, y),VARS =list(y = y))str(res)library(memshare)library(parallel)list_length=1000matrix_dim=100ListV=lapply(1:list_length,function(i)matrix(rnorm(matrix_dim* matrix_dim),nrow = matrix_dim,ncol = matrix_dim))y=rnorm(matrix_dim)namespace="ID123"res= memshare::memLapply(ListV,function(el, y) { el%*% y},NAMESPACE=namespace,VARS=list(y=y),MAX.CORES =1)Each element el of ListV is multiplied by y in parallel. The listresides once in shared memory.
Pages: memory regions owned by the current R session that loadedthe package.
Views: ALTREP wrappers exposing shared memory variables(read/write capable).
Namespaces: string identifiers defining a shared memory contextacross sessions.
When the package is detached, all handles and associated sharedmemory pages are released, unless another R process still holdsreferences.
memshare exposes explicit lifecycle functions so you cancontrol when data is placed in shared memory and when it is freed.
registerVariables(namespace, variableList)Allocate shared memory and copy R objects (matrices or vectors, orlists formemLapply) into it. -namespace:character(1). Identifier of the shared memory context shared acrossprocesses. -variableList: anamed list ofobjects to register. Names become the keys under which you can laterretrieve views.
Example
library(memshare)ns<-"my_namespace"X<-matrix(rnorm(1e4),100,100)y<-rnorm(100)registerVariables(ns,list(X = X,y = y))# Now X and y live once in shared memory and can be accessed from other R processesreleaseVariables(namespace, variableNames)Delete variables from the shared memory space. Shared regions areonly removed whenno active views remain. -namespace: character(1) used above. -variableNames: character vector of variable names tofree.
Example
# After all workers have released their views:releaseVariables(ns,c("X","y"))retrieveViews() andreleaseViews()To avoid duplication, workers attach to shared memory byviews: -retrieveViews(namespace, c("X","y")) returns ALTREP-backedobjects that behave like ordinary R matrices/vectors. - Always callreleaseViews(namespace, ...) when finished so that thebacking memory can be reclaimed.
Example (worker-side)
vlist<-retrieveViews(ns,c("X","y"))# use vlist$X and vlist$yreleaseViews(ns,c("X","y"))Tip:
memApply()andmemLapply()manageviews for you automatically, but the low-level API above is useful forcustom workflows.
The full manual for users or developers is available here:Packagedocumentation
[Thrun and Märte, 2025] Thrun, M.C., Märte, J.: Memshare: MemorySharing for Multicore Computation in R with an Application to FeatureSelection by Mutual Information using PDE, 2025.
[Thrun et al., 2020] Thrun, M.C., Gehlert, T., & Ultsch, A.:Analyzing the Fine Structure of Distributions, PLOS ONE, 15(10),e0238835, 2020.
[Ultsch, 2005] Ultsch, A.: Pareto Density Estimation: A DensityEstimation for Knowledge Discovery, Proceedings of the 28th AnnualConference of the German Classification Society, Springer, 2005