Skip to content
Prev 9690 / 63421 Next

'methods' and environments.

Laurent Gautier wrote:
The memory growth seems real, but its connection to "environments as
slots" is unclear.

The only recent change that sounds relevant is the modification to
ensure that methods are evaluated in an environment that reflects the
lexical scope of the method's definition.  That does create a new
environment for each call to a generic function, but has nothing to do
with slots being environments.

It's possible there is some sort of "memory leak" or extra copying
there, but I'm not familiar enough with the details of that code to say
for sure.

Notice that the following workaround has no bad effects on memory
(suggesting that the extra environment in evaluating generics may in
fact be relevant):

R> setClass("A", representation(a="matrix"))
[1] "A"
R> aa <- matrix(600^2, 50)
R> a1 <- new("A")
R> a1@a <- aa
R> gc()
         used (Mb) gc trigger (Mb)
Ncells 370247  9.9     531268 14.2
Vcells  87522  0.7     786432  6.0



The general solution for dealing with large objects is likely to involve
some extensions to R to allow "reference" objects, for which the
programmer is responsible for any copying.

Environments themselves are not quite adequate for this purpose, since
different "references" to the same environment cannot have different
attributes.

John