rshared-memoryr-bigmemory

R bigmemory: how to access matrix/keep in shared memory after script got executed


I use the bigmemory package to put a very large matrix into shared memory (see script below, so it can be accessed in parallel by scripts in other R sessions.

I now want to execute the script in a non-interactive way. The problem is, that if I run it with Rscript, the matrix is removed from shared memory right after the Rscript process ended. I could add Sys.sleep(99999) to the end of the script, but I am wondering if there is any better way to acclompish this. Any ideas?

library(bigmemory)

m = read.big.matrix("matrix.txt", type='double', shared = TRUE, header = FALSE, sep = "\t")

sign = describe(m)
dput(sign, "matrix.signature")

Solution

  • As long as the matrix is attached in at least one R session it remains in an isolated part of the RAM. The best practice to prevent the matrix from being lost is to fileback it. There is no performance penalty involved, but you have to be aware that in the designated location on your hard drive, these data reside and remain there until you delete them explicitly. Even closing all R sessions won't delete them. The finalizer is deactivated in this case.