Compute the size of an object.

Share:

Description

object_size works similarly to object.size, but counts more accurately and includes the size of environments. compare_size makes it easy to compare the output of object_size and object.size.

Usage

1
2
3

Arguments

env

Environment in which to terminate search. This defaults to the current environment so that you don't include the size of objects that are already stored elsewhere.

x,...

Set of objects to compute total size.

Value

An estimate of the size of the object, in bytes.

Environments

object_size attempts to take into account the size of the environments associated with an object. This is particularly important for closures and formulas, since otherwise you may not realise that you've accidentally captured a large object. However, it's easy to over count: you don't want to include the size of every object in every environment leading back to the emptyenv(). object_size takes a heuristic approach: it never counts the size of the global env, the base env, the empty env or any namespace.

Additionally, the env argument allows you to specify another environment at which to stop. This defaults to the environment from which object_size is called to prevent double-counting of objects created elsewhere.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# object.size doesn't keep track of shared elements in an object
# object_size does
x <- 1:1e4
z <- list(x, x, x)
compare_size(z)

# this means that object_size is not transitive
object_size(x)
object_size(z)
object_size(x, z)

# object.size doesn't include the size of environments, which makes
# it easy to miss objects that are carrying around large environments
f <- function() {
  x <- 1:1e4
  a ~ b
}
compare_size(f())

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.