user1134616 user1134616 - 3 months ago 26
R Question

Measuring memory usage in R

Consider two vectors:

a <- 1
b <- 1:100


Why is the result of this line of code:

sum(sapply(ls(),function(x){utils::object.size(get(x))}))
#488 (bytes)


different from this line of code:

utils::object.size(ls())
#152 bytes

Answer

The first version is giving you the correct result:

object.size(a)  ## 32 Bytes
object.size(b)  ## 424 Bytes
sum(sapply(ls(),function(x){utils::object.size(get(x))}))  ## 456 Bytes

While the second, is doing essentially:

object.size(c("a", "b"))  ## 96 Bytes
Comments