[R] auditing R's memory usage

jim holtman jholtman at gmail.com
Fri May 4 21:38:14 CEST 2012


Here is a function I use to look at the size of objects:

######################start
my.ls <- function (pos = 1, sorted = FALSE, envir = as.environment(pos))
{
    .result <- sapply(ls(envir = envir, all.names = TRUE),
function(..x) object.size(eval(as.symbol(..x),
        envir = envir)))
    if (sorted) {
        .result <- rev(sort(.result))
    }
    .ls <- as.data.frame(rbind(as.matrix(.result), `**Total` = sum(.result)))
    names(.ls) <- "Size"
    .ls$Size <- formatC(.ls$Size, big.mark = ",", digits = 0,
        format = "f")
    .ls$Mode <- c(unlist(lapply(rownames(.ls)[-nrow(.ls)], function(x)
mode(eval(as.symbol(x),
        envir = envir)))), "-------")
    .ls
}
#########################end







> # create some large objects
> x <- matrix(0, 3000, 3000)
> my.ls()
              Size    Mode
x       72,000,112 numeric
**Total 72,000,112 -------
> gc()
           used (Mb) gc trigger (Mb) max used (Mb)
Ncells   684078 18.3    1368491 36.6  1368491 36.6
Vcells 10198339 77.9   11581078 88.4 10200979 77.9
> # make another one
> y <- matrix(0, 5000, 5000)
> my.ls()
               Size    Mode
x        72,000,112 numeric
y       200,000,112 numeric
**Total 272,000,224 -------
> gc()
           used  (Mb) gc trigger  (Mb) max used  (Mb)
Ncells   684087  18.3    1368491  36.6  1368491  36.6
Vcells 35198359 268.6   39143593 298.7 35201440 268.6
>




On Fri, May 4, 2012 at 12:49 PM, Alexander Shenkin <ashenkin at ufl.edu> wrote:
> Hi Folks,
>
> I'm running 32-bit R 2.14 in RStudio on my Win 7 x64 system with 8GB
> RAM.  I'm getting memory problems as R wants to swallow more than the
> 4GB limit.
>
> I think I'm stuck at 4GB as I have to use 32-bit R for a number of
> packages (ODBC, etc).  However, I doubt I really need to be using that
> much memory - I'm probably being very sloppy in my memory management,
> leaving lots of temporary dataframes around, etc.
>
> I'm looking for suggestions about how to audit R's memory usage.  Yes, I
> could go around my code and tie up every loose end.  But in the interest
> of efficiency, I'm wondering about ways I might more intelligently audit
> R's memory usage.  Are there any tools that can tell me what objects are
> swallowing what amounts of memory?  Armed with that information, I can
> go track down the worst culprits.
>
> Thanks,
> Allie
>
> ______________________________________________
> R-help at 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.



More information about the R-help mailing list