[R] Memory management

Charilaos Skiadas skiadas at hanover.edu
Wed Apr 11 15:19:21 CEST 2007

Before you go down that road, I would recommend first seeing if it is  
really a problem. Premature code optimization is in my opinion never  
a good idea.

Also, reading the Details on ?attach you will find this:

	The database is not actually attached. Rather, a new environment is  
created on the search path and the elements of a list (including  
columns of a data frame) or objects in a save file or an environment  
are copied into the new environment. If you use <<- or assign to  
assign to an attached database, you only alter the attached copy, not  
the original object. (Normal assignment will place a modified version  
in the user's workspace: see the examples.) For this reason attach  
can lead to confusion.

So in fact it is the attaching that has to do copying, not the other  
way around.

As for references, perhaps there is a better one, but searching for  
"pass" in "Writing R Extensions" I found the following on page 41:

	Some memory allocation is obvious in interpreted code, for example,
y <- x + 1
allocates memory for a new vector y. Other memory allocation is less  
obvious and occurs because
R is forced to make good on its promise of ‘call-by-value’ argument  
passing. When an argument
is passed to a function it is not immediately copied. Copying occurs  
(if necessary) only when
the argument is modified. This can lead to surprising memory use.

Perhaps a better source, section 4.3.3 of "The R language  
definition", on Argument Evaluation.

On Apr 11, 2007, at 8:25 AM, yoooooo wrote:

> I guess I have more reading to do.... Are there any website that I  
> can read
> up on memory management, or specifically what happen when we 'pass in'
> variables, which strategy is better at which situation?
> Thanks~
> - yoooooooo
>> On Tue, 10 Apr 2007, yoooooo wrote:
>>> Hi all, I'm just curious how memory management works in R... I  
>>> need to
>>> run an
>>> optimization that keeps calling the same function with a large  
>>> set of
>>> parameters... so then I start to wonder if it's better if I  
>>> attach the
>>> variables first vs passing them in (coz that involves a lot of  
>>> copying..
>>> )

Haris Skiadas
Department of Mathematics and Computer Science
Hanover College

More information about the R-help mailing list