Learn R Programming

reproducible (version 1.1.1)

objSize: Recursive object.size

Description

This has methods for various types of things that may not correctly report their object size using object.size. Also, for lists and environments, it will return the object size separately for each element. These are estimates only, and could be inaccurate. Alternative, similar functions include object.size and pryr::object_size. See Details for the special case of functions and their enclosing environments.

Usage

objSize(x, quick, enclosingEnvs, .prevEnvirs, ...)

# S3 method for default objSize( x, quick = getOption("reproducible.quick", FALSE), enclosingEnvs = TRUE, .prevEnvirs = list(), ... )

# S3 method for list objSize( x, quick = getOption("reproducible.quick", FALSE), enclosingEnvs = TRUE, .prevEnvirs = list(), ... )

# S3 method for environment objSize( x, quick = getOption("reproducible.quick", FALSE), enclosingEnvs = TRUE, .prevEnvirs = list(), ... )

# S3 method for Path objSize( x, quick = getOption("reproducible.quick", FALSE), enclosingEnvs = TRUE, .prevEnvirs = list(), ... )

# S3 method for `function` objSize( x, quick = getOption("reproducible.quick", FALSE), enclosingEnvs = TRUE, .prevEnvirs = list(), ... )

objSizeSession(sumLevel = Inf, enclosingEnvs = TRUE, .prevEnvirs = list())

Arguments

x

An object

quick

Logical. Only some methods use this. e.g., Path class objects. In which case, file.size will be used instead of object.size.

enclosingEnvs

Logical indicating whether to include enclosing environments. Default TRUE.

.prevEnvirs

For internal account keeping to identify and prevent duplicate counting

...

Additional arguments (currently unused)

sumLevel

Numeric, indicating at which depth in the list of objects should the object sizes be summed (summarized). Default is Inf, meaning no sums. Currently, the only option other than Inf is 1: objSizeSession(1), which gives the size of each package.

Details

For functions, a user can include the enclosing environment as described https://www.r-bloggers.com/using-closures-as-objects-in-r/ and http://adv-r.had.co.nz/memory.html. It is not entirely clear which estimate is better. However, if the enclosing environment is the .GlobalEnv, it will not be included even though enclosingEnvs = TRUE.

objSizeSession will give the size of the whole session, including loaded packages. Because of the difficulties in calculating the object size of base and methods packages and Autoloads, these are omitted.

Examples

Run this code
# NOT RUN {
library(utils)

foo <- new.env()
foo$b <- 1:10
foo$d <- 1:10

objSize(foo) # all the elements in the environment
object.size(foo) # different - only measuring the environment as an object

object.size(prepInputs) # only the function, without its enclosing environment
objSize(prepInputs)     # the function, plus its enclosing environment

# Size of all packages; includes their imported functions
# }
# NOT RUN {
  bar <- objSizeSession(1)
  print(bar, units = "auto")
# }
# NOT RUN {
os1 <- object.size(as.environment("package:reproducible"))
os2 <- objSize(as.environment("package:reproducible"))
(os1) # very small -- just the environment container
sum(unlist(os2)) # around 13 MB, with all functions, objects
                 # and imported functions

# }

Run the code above in your browser using DataLab