Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Complete R Session Size

Tags:

r

session

Due to I constantly reach memory size limit in my R Session (8GB Windows PC) I start to remove big objects loaded in. However once I reach this limit, removing objects seems not to work.

So, I was wondering if there's a way to get the R Session size. I know that it's possible to retrieve objects' size (saw in this thread).I want to know if there's a way to count the complete R Session size though (loaded packages, objects, etc).

Thank you!

like image 731
patL Avatar asked Oct 11 '17 14:10

patL


People also ask

Why is my R session using so much memory?

R uses more memory probably because of some copying of objects. Although these temporary copies get deleted, R still occupies the space. To give this memory back to the OS you can call the gc function. However, when the memory is needed, gc is called automatically.

How to check memory usage in R?

1600, the RStudio IDE includes a small memory usage widget in the Environment pane. This widget helps you track the amount of memory your R session is using. The pie chart shows the total system memory usage; that is, if your system has 8GB of RAM and 4GB is currently in use, it will show at 50%.

How do I find the size of an object in R?

To find the object size in R, we can use object. size function. For example, if we have a data frame called df then the size of df can be found by using the command object. size(df).

What does gc do in R?

R uses an alternative approach: garbage collection (or GC for short). GC automatically releases memory when an object is no longer used. It does this by tracking how many names point to each object, and when there are no names pointing to an object, it deletes that object.


1 Answers

I personally use this function to get the available memory:

getAvailMem <- function(format = TRUE) {

  gc()

  if (Sys.info()[["sysname"]] == "Windows") {
    memfree <- 1024^2 * (utils::memory.limit() - utils::memory.size())
  } else {
    # http://stackoverflow.com/a/6457769/6103040
    memfree <- 1024 * as.numeric(
      system("awk '/MemFree/ {print $2}' /proc/meminfo", intern = TRUE))
  }

  `if`(format, format(structure(memfree, class = "object_size"),
                      units = "auto"), memfree)
}
like image 54
F. Privé Avatar answered Sep 22 '22 05:09

F. Privé