I can give you the answer to #1. If you had put a "print(str(m))" you would have seen that initially the matrix was setup as logical which requires 4 bytes per element. On the first assignment of a numeric, the mode of 'm' is changed to numeric which requires 8 bytes per element; that is the reason for the "doubling".
On Mon, Jul 15, 2013 at 6:50 PM, ivo welch <ivo.we...@anderson.ucla.edu>wrote: > dear R experts: I am curious again about R memory allocation strategies. > Consider an intentionally inefficient program: > > ranmatme <- function( lx, rx ) { > m <- matrix(NA, nrow=lx, ncol=rx) > for (li in 1:rx) { > cat("\tLag i=", li, "object size=", object.size(m), "\n") > m[,li] <- rnorm(lx) > } > m > } > > v <- ranmatme( 1024*1024*128, 3 ) > > > [1] on the first cat, the object size is only 1.6GB, which is half the size > of the 3.2GB that it is on the 2nd and 3rd call. why? > > [2] I tried to monitor the linux memory allocation in another window. I > could be completely wrong, but it seems that upon function exit, memory > usage spikes briefly. it is almost as if there was an explicit copy of m > into v, and both had to exist simultaneously for a moment in time. is this > the case? (if so, is there a way to return and assign just the reference? > I may be blanking here---maybe the answer is obvious.) > > regards, > > /iaw > ---- > Ivo Welch (ivo.we...@gmail.com) > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > -- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it. [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.