[R-pkg-devel] Run garbage collector when too many open files

Jan van der Laan rhelp @ending from eoo@@dd@@nl
Tue Aug 7 14:26:35 CEST 2018



In my package I open handles to temporary files from c++, handles to 
them are returned to R through vptr objects. The files are deleted then 
the corresponding R-object is deleted and the garbage collector runs:

a <- lvec(10, "integer")
rm(a)

Then when the garbage collector runs the file is deleted. However, on 
some platforms (probably with lower limits on the maximum number of file 
handles a process can have open), I run into the problem that the 
garbage collector doesn't run often enough. In this case that means that 
another package of mine using this package generates an error when its 
tests are run.

The simplest solution is to add some calls to gc() in my tests. But a 
more general/automatic solution would be nice.

I thought about something in the lines of

robust_lvec <- function(...) {
   tryCatch({
     lvec(...)
   }, error = function(e) {
     gc()
     lvec(...) # duplicated code
   })
}

e.g. try to open a file, when that fails call the garbage collector and 
try again. However, this introduces duplicated code (in this case only 
one line, but that can be more), and doesn't help if it is another 
function that tries to open a file.

Is there a better solution?

Thanks!

Jan



More information about the R-package-devel mailing list