I use the bigmemory package to access big matrix objects in parallel, e.g. like this
a <- bigmemory::big.matrix(nrow = 200, ncol = 100, shared = TRUE) # shared = TRUE is the default
However, working with the resulting objects sometimes cause R to crash. This means that the memory used by the matrix objects is not released. The bigmemory manual warns of such a case but presents no solution:
Abruptly closed R (using e.g. task manager) will not have a chance to finalize the big.matrix objects, which will result in a memory leak, as the big.matrices will remain in the memory (perhaps under obfuscated names) with no easy way to reconnect R to them
After a few crashes and restarts of my R process, I get the following error:
No space left on device Error in CreateSharedMatrix(as.double(nrow), as.double(ncol), as.character(colnames), : The shared matrix could not be created
Obviously, my memory is blocked by orphaned big matrices. I tried the command ipcs, which is advertised to list shared memory blocks, but the sizes of the segments listed there are much too small compared to my matrix objects. This means also that ipcrm is of no use here to remove my orphaned objects.
Where does bigmemory store its objects on different operating systems and how do I delete orphaned ones?
Linux
A call to
df -hsolved the mystery for my operating system (Linux/CentOS).$ df -h
There is a temporary file system in the folder
/dev/shm. Files therein exist only in RAM. This file system is used to share data between processes. In this folder were several files with random strings as names, and multiple files with the same prefix, which seem to be related to the samebig.matrixobject:Unfortunately, I don't know which matrix belongs to which file, but if you have no R processes running at the time, deleting files with this name pattern should remove the orphaned objects.
Windows
I don't know how other OS's do this, so feel free to add it into this community wiki if you know