[Rd] Memory leak with tons of closed connections

Gergely Daróczi daroczig at rapporter.net
Thu Nov 10 16:48:12 CET 2016

Dear All,

I'm developing an R application running inside of a Java daemon on
multiple threads, and interacting with the parent daemon via stdin and

Everything works perfectly fine except for having some memory leaks
somewhere. Simplified version of the R app:

    while (TRUE) {
        con <- file('stdin', open = 'r', blocking = TRUE)
        line <- scan(con, what = character(0), nlines = 1, quiet = TRUE)

This loop uses more and more RAM as time passes (see more on this
below), not sure why, and I have no idea currently on how to debug
this further. Can someone please try to reproduce it and give me some
hints on what is the problem?

Sample bash script to trigger an R process with such memory leak:

    Rscript --vanilla -e "while(TRUE)cat(runif(1),'\n')" | Rscript
--vanilla -e "cat(Sys.getpid(),'\n');while(TRUE){con<-file('stdin',open='r',blocking=TRUE);line<-scan(con,what=character(0),nlines=1,quiet=TRUE);close(con);rm(con);gc()}"

Maybe you have to escape '\n' depending on your shell.

Thanks for reading this and any hints would be highly appreciated!


PS1 see the image posted at
on memory usage over time
PS2 the issue doesn't seem to be due to writing more data in the first
R app compared to what the second R app can handle, as I tried the
same with adding a Sys.sleep(0.01) in the first app and that's not an
issue at all in the real application
PS3 I also tried using stdin() instead of file('stdin'), but that did
not work well for the stream running on multiple threads started by
the same parent Java daemon
PS4 I've tried this on Linux using R 3.2.3 and 3.3.2

More information about the R-devel mailing list