From rhelp @ending from eoo@@dd@@nl  Tue Aug  7 15:19:55 2018
From: rhelp @ending from eoo@@dd@@nl (Jan van der Laan)
Date: Tue, 7 Aug 2018 15:19:55 +0200
Subject: [Rd] Run garbage collector when too many open files
Message-ID: <7484b808-0993-6760-b5ac-ec8dd52e982e@eoos.dds.nl>



In my package [1] I open handles to temporary files from c++, pointer to 
the objects containing those handles are returned to R as external 
pointer objects. The files are deleted when 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, for 
example, that another package of mine using this package generates an 
error when its tests are run [2].

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


[1] https://cran.r-project.org/web/packages/lvec/index.html
[2] https://cran.r-project.org/web/checks/check_results_reclin.html



[Rd] Run garbage collector when too many open files
Jan van der Laan