[R] calculating memory usage

Roger D. Peng rpeng at jhsph.edu
Tue Sep 14 16:10:27 CEST 2004

If you only have simple objects in your function, you might be able to 
use a function like

totalMem <- function()  {
   sum(sapply(ls(all = TRUE), function(x) object.size(get(x)))) / 2^20

which should give you a rough idea of the memory usage (in MB) in the 
current environment.


Adaikalavan Ramasamy wrote:
> I am comparing two different algorithms in terms of speed and memory
> usage. I can calculate the processing time with proc.time() as follows
> but am not sure how to calculate the memory usage.
>    ptm <- proc.time()
>    x <- rnorm(1000000)
>    proc.time() - ptm
> I would like to be within R itself since I will test the algorithm
> several hundred times and in batch mode. So manually looking up 'top'
> may not be feasible. help.seach("memory") suggests memory.profile and gc
> but I am not sure how to use these.
> Sorry if this is a basic question. Thank you.
> Regards, Adai
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

More information about the R-help mailing list