Skip to content
Prev 138391 / 398506 Next

renaming objects

On Mon, 3 Mar 2008, Nordlund, Dan (DSHS/RDA) wrote:
[..., quoting Hadley Wickham]
Hadley was correct: it is not prevented by removing 'a', as R does not 
have reference counting.   E.g.

rm(a)
b[1] <- 1
gc()
            used (Mb) gc trigger  (Mb) max used  (Mb)
Ncells   133947  3.6     350000   9.4   350000   9.4
Vcells 10087573 77.0   21337085 162.8 20087562 153.3

Note the 'max used' Vcells.

There's a fairly complete explanation of what happens in the 'R Internals' 
manual.

I think the most common source of confusion is over the term 'objects'.  R 
does not have 'objects' in this sense: 'a' and 'b' are symbols with 
bindings to values.  So you cannot change 'b', but you can change its 
binding.  When you do b[1] <- 1 you may create a new C-level structure as 
the new value, or you may change the existing one.  In this case it 
created a new structure (by copying the old one and altering that). 
Certain replacement functions are the only way to avoid making a new 
value: a <- a+0 for example always creates a new value (at a different 
address in memory) even though its contents will be identical.

Once we get away from the simplest vectors more sharing can be done: e.g. 
character vectors with duplicate elements will share storage for those 
elements.