How to determine the size of all objects in the current workspace in R? (not in WIndows)

On Windows, only one can do memory.size()to get the total amount of memory consumed by the (current) current Rsession.

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

But how to make an equivalent print( object.size( ---workspace--- ))?

Looping for (thing in ls()) print( object.size( thing ), units='auto' )prints incorrect output, for example:

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

what i did not mean.

+3
source share
3 answers

The correct way to do this is:

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

, get, , , , .

+4

, :

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

workspace.size()
# 35192 bytes
+4

This gives a well 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
0
source

All Articles