9

In Windows only one can do memory.size() to get the total amount of memory eaten up by the (objects in the) current R session.

It's also possible to understand the size of an individual object with print( object.size( thing ), units='auto') which says how many megabytes/kilobytes that particular data-frame/table takes up.

But how to do the equivalent of print( object.size( ---workspace--- ))?

Looping for (thing in ls()) print( object.size( thing ), units='auto' ) prints the wrong output, such as:

64 bytes
72 bytes
88 bytes
88 bytes
64 bytes
64 bytes
64 bytes
64 bytes
64 bytes
64 bytes
64 bytes
64 bytes
64 bytes
64 bytes
72 bytes
88 bytes
64 bytes
64 bytes
64 bytes
64 bytes
64 bytes
64 bytes
64 bytes
64 bytes
64 bytes
64 bytes
64 bytes

which is not what I meant.

isomorphismes
  • 8,233
  • 9
  • 59
  • 70
  • I've googled this question numerous times. Posting the answer here so someone else will find it. My answer is just an adaption of http://stackoverflow.com/a/10383199/563329, but a searcher might not use terms that lead them to that SO question. – isomorphismes Feb 25 '14 at 18:54

5 Answers5

7

The correct way to do it is:

for (thing in ls()) {
    print(
      object.size(
        get(thing)
        ),
      units='auto')
    }

which is just slightly different in that the loop uses get to specify that it's the size of the object itself, not the name of the object, that should be measured.

isomorphismes
  • 8,233
  • 9
  • 59
  • 70
7

To print the size of the whole workspace, you could try the following function:

workspace.size <- function() {
  ws <- sum(sapply(ls(envir=globalenv()), function(x)object.size(get(x))))
  class(ws) <- "object_size"
  ws
}

workspace.size()
# 35192 bytes
sgibb
  • 25,396
  • 3
  • 68
  • 74
  • Note that this isn't equivalent to `memory.size` and will be an underestimate. For instance, in a vanilla workspace, this gives me ~8000 bytes, while `memory.size` gives ~25MB. Including `baseenv()` gets a little closer, but overshoots slightly. – Matthew Plourde Feb 25 '14 at 19:17
  • 1
    ...and we should note that `object.size()` is a rough estimate of the occupied space. If there are 2 _shared_ objects in the global environment, they will be counted twice... – gagolews Feb 25 '14 at 19:18
  • And there will be lots of shared objects if you have any character vectors. – hadley Feb 25 '14 at 21:33
  • See my reply for a generalized version of this. – Jonas Lindeløv Apr 21 '21 at 13:02
4

This gives a nicely-formatted output:

size = 0
for (x in ls() ){
    thisSize = object.size(get(x))
    size = size + thisSize
    message(x, " = ", appendLF = F); print(thisSize, units='auto')
}
message("total workspace is ",appendLF = F); print(size, units='auto')

Like this:

a = 3.7 Kb
ACE1 = 244.3 Kb
etc..
zfact_o = 48 bytes

total is 130.9 Mb
tim
  • 3,559
  • 1
  • 33
  • 46
1

Building on the answer by @sgibb, the function below computes the size of any environment. Like the other solutions posted here, it does not include nested environments, e.g., R6 classes.

env.size <- function(env = globalenv()) {
  env_vars = ls(envir = env)
  if (length(env_vars) == 0) {
    bytes = 0
  } else if (length(env_vars) == 1) {
    bytes = object.size(get(env_vars, envir = env))
  } else {
    bytes = sum(sapply(env_vars, function(x) object.size(get(x, envir = env))))
  }
  class(bytes) = "object_size"
  bytes
}

For example:

# defaults to global environment
env.size()
# 219240 bytes

# another environment
my_env = new.env()
my_env$x = rnorm(10)
env.size(my_env)
# 176 bytes
Jonas Lindeløv
  • 5,442
  • 6
  • 31
  • 54
0

I used the following code adapted from @isomophismes:

data.frame(object = ls())%>%
mutate(size = map(objects = ~object.size(get(.x))))

This makes a dataframe which you call in rGUI to see what size objects are

JisL
  • 161
  • 8