[R] growing a list sequentially -- memory management

Duncan Murdoch murdoch at stats.uwo.ca
Fri Nov 14 18:40:50 CET 2008

On 11/14/2008 12:33 PM, erwann rogard wrote:
> Thank you all for your answers. I think I have enough to keep going:
> As pointed out it is probably not growing a list of objects each of size
> 6824 bytes that is the source of the problem per se (even if the list is not
> pre-allocated), but rather a "memory leak" with the function fun within the
> loop. I'm not providing the full example here because the code spans several
> files.
> Nonetheless the body of fun itself contains deeply nested lists, some which
> are temporary and others which are part of the return value. So i'm now
> trying to see what to do about these nested lists instead. About memory
> leak: my assumption was that the memory needed for a temporary object (i.e.
> defined within the body of a function) would be redeemed after the function
> call, but perhaps i'm wrong about that (no automatic garbage collection)?

It depends on the function.  Functions can define and return functions, 
and in that case, the environment of the returned function will contain 
all the locals.  For example:

 > MakeF <- function() {
+    x <- rnorm(10000)
+    y <- rnorm(10000)
+    f <- function() {}
+    return(f)
+ }
 > F <- MakeF()
 > ls(environment(F))
[1] "f" "x" "y"

F refers to its environment, which is the evaluation frame of the call 
to MakeF, so x and y can't be garbage collected, even though F never 
refers to them directly.

Duncan Murdoch

More information about the R-help mailing list