Skip to content
Prev 52596 / 63424 Next

Memory leak with tons of closed connections

On Fri, Nov 11, 2016 at 12:46 PM, Gergely Dar?czi
<daroczig at rapporter.net> wrote:
[...]
Yes, R does not know about it, it does not manage this memory (any
more), but the R process requested this memory from the OS, and never
gave it back, which is basically the definition of a memory leak. No?

I think the leak is because 'stdin' is special and R opens it with fdopen():
https://github.com/wch/r-source/blob/f8cdadb769561970cc42776f563043ea5e12fe05/src/main/connections.c#L561-L579

and then it does not close it:
https://github.com/wch/r-source/blob/f8cdadb769561970cc42776f563043ea5e12fe05/src/main/connections.c#L636

I understand that R cannot fclose the FILE*, because that would also
close the file descriptor, but anyway, this causes a memory leak. I
think.

It seems that you cannot close the FILE* without closing the
descriptor, so maybe a workaround would be to keep one FILE* open,
instead of calling fdopen() to create new ones every time. Another
possible workaround is to use dup(), but I don't know enough about the
details to be sure.

Gabor

[...]