[Rd] Too many files open (PR#13818)

rm.short at comcast.net rm.short at comcast.net
Fri Jul 10 19:20:15 CEST 2009

Full_Name: Richard Short
Version: 2.9.1
OS: Windows XP
Submission from: (NULL) (

I'm having a problem in R that may be an operating system problem. I'm getting
an error message that reads, "Too many files open". I'm opening files and
closing them (and unlinking them), but when I go through that process 509 times,
the program bombs and I get this error message. I've been working on this
problem for a week and have gleaned a bit of info from different threads, but no
solutions yet. There is some talk that some windows systems have a limit of 512
files that can be open at one time. Even though I'm closing my files each time,
something is keeping track of how many time I've opened and closed a file in a
session and halts execution after 512 occurances.

I've attached a script that will induce the problem. However, be warned that you
will have to get out of R after you run it. R will hang up. It won't be obvious
at first, but you will no longer be able to open files (or help). This can be
solved by getting out of the R GUI and back in. Also, I'm using my pathname,
"MyPathnameA", so you'll probably want to edit it with your own.
R E-mails from as far back as 2006 ask for help on the issue. There have been
several suggestions, but no confirmed solution that I can find. You will see my
attempt at these suggestions in the script [ rm(outMIA);rm(junk); and gc();
after close(outMIA); and unlink(FileNameMIA);]. 

#-------------------- Start of Script
# This script induces an error: "cannot open the connection" with
# warnings: "Too many open files". 

       # ---------------- Create a junk file and "save" it for use further into
the program ---
       junk <- 1
       MyPathnameA <- "C:\\Documents and Settings\\All
       connectionX <- paste(MyPathnameA,"junk",sep="\\")
       outJunk     <- file(connectionX, open="wb")
       save(junk, file=outJunk)

       # The next two lines are a repeat from above. They will be useful if you
want to run
       # this script again after "junk" has been loaded.
       MyPathnameA <- "C:\\Documents and Settings\\All
       connectionX <- paste(MyPathnameA,"junk",sep="\\")
       FileNameMIA <- connectionX 
       for(i in 1:4000){
            # ----------------- Load "junk" ---------------------------------
            outMIA <- file(FileNameMIA, open="rb")
            # ----------------- close "junk"; unlink; remove; ----------------
            cat(" i = ",i,sep=" ")
            zzz <- showConnections(all=FALSE)
            cat(" zzz = ",zzz,"\n",sep=" ")
#------------------------------------ End of Script

Any suggestion would be greatly appreciated.

More information about the R-devel mailing list