title: Memory leak with closed connections
body: <p>I have an R application interacting with a Java daemon via <code>stdin</code> and <code>stdout</code> in an infinite loop, which seems to have some memory leaks. Simplified R app:</p>
  
  <pre><code>while (TRUE) {
      con &lt;- file('stdin', open = 'r', blocking = TRUE)
      line &lt;- scan(con, what = character(0), nlines = 1, quiet = TRUE)
      close(con)
  }
  </code></pre>
  
  <p>This loop ends up using more and more RAM, and even if I manually <code>gc()</code> after the <code>close(con)</code> call, the memory footprint seems to be OK for a while, but eventually grows forever.</p>
  
  <p>A basic script to confirm this:</p>
  
  <pre><code>Rscript --vanilla -e "while(TRUE)cat(runif(1),'\n')" | Rscript --vanilla -e "cat(Sys.getpid(), '\n');while (TRUE) {con &lt;- file('stdin', open = 'r', blocking = TRUE);line &lt;- scan(con, what = character(0), nlines = 1, quiet = TRUE);close(con);gc()}"
  </code></pre>
  
  <p>This will start two R processes: one writing to <code>stdout</code> and the other reading from <code>stdin</code> connected with a pipe (and the second printing the <code>pid</code> so that you can monitor the related memory usage):</p>
  
  <p><a href="https://i.stack.imgur.com/xuL68.png" rel="noreferrer"><img src="https://i.stack.imgur.com/xuL68.png" alt="enter image description here"></a></p>
  
  <p>I'm not sure what I'm doing wrong, but would love to stop this memory leak so any help is highly appreciated.</p>