I want to monitor my memory usage in RStudio so that I can avoid getting out-of-memory errors on the cluster. I'm looking for a method to calculate peak memory usage that includes both global variables and local variables. For example, the peak memory usage should account for intermediate variables in functions and apply loops.
gc(reset = T)
sum(gc()[, "(Mb)"]) # 172Mb
lapply(1:3, function(x) {
mx <- replicate(10, rnorm(1e6)) # 80Mb object
mean(mx)
})
sum(gc()[, "(Mb)"]) # 172Mb -- still the same!