object_size: Report the Space Allocated for Objects

View source: R/object_size.R

object_sizeR Documentation

Report the Space Allocated for Objects

Description

Provides an estimate of the memory that is being used to store R objects.

Usage

object_size(...)

## S3 method for class 'object_sizes'
is(x)

## S3 method for class 'object_sizes'
as(x)

## S3 method for class 'object_sizes'
c(..., recursive=FALSE)

## S3 method for class 'object_sizes'
format(x, humanReadable=getOption("humanReadable"),
       standard="IEC", units, digits=1, width=NULL, sep=" ",
       justify=c("right", "left"), ...)

## S3 method for class 'object_sizes'
print(x, quote=FALSE,
      humanReadable=getOption("humanReadable"), standard="IEC", units, digits=1,
      width=NULL, sep=" ", justify=c("right", "left"), ...)

Arguments

...

object_size: R objects; print and format: arguments to be passed to other methods.

x

output from object_size.

quote

whether or not the result should be printed with surrounding quotes.

humanReadable

whether to use the “human readable” format.

standard, units, digits, width, sep, justify

passed to humanReadable.

recursive

passed to the c method.

Details

The base R package utils provides an object.size function that handles a single object. The gdata package provides a similar object_size function that handles multiple objects.

Both the utils and gdata implementations store the object size in bytes, but offer a slightly different user interface for showing the object size in other formats. The gdata implementation offers human readable format similar to ls, df or du shell commands, by calling humanReadable directly, calling print with the argument humanReadable=TRUE, or by setting options(humanReadable=TRUE).

The 3.0.0 release of gdata renamed this function to object_size, allowing users to explicitly call the gdata implementation. This eliminates ambiguity and makes it less likely to get errors when running parts of an existing script without first loading the gdata package. The old object.size function name is now deprecated in the gdata package, pointing users to object_size and utils::gdata instead.

Value

A numeric vector class c("object_sizes", "numeric") containing estimated memory allocation attributable to the objects in bytes.

See Also

object.size in package 'utils' for the base R implementation, Memory-limits for the design limitations on object size, humanReadable for human readable format.

Examples

object_size(letters)
object_size(ls)

# Find the 10 largest objects in the base package
allObj <- sapply(ls("package:base"), function(x)
  object_size(get(x, envir=baseenv())))
(bigObj <- as.object_sizes(rev(sort(allObj))[1:10]))
print(bigObj, humanReadable=TRUE)

as.object_sizes(14567567)

oldopt <- options(humanReadable=TRUE)
(z <- object_size(letters,
                  c(letters, letters),
                  rep(letters, 100),
                  rep(letters, 10000)))
is.object_sizes(z)
as.object_sizes(14567567)
options(oldopt)

# Comparison
# gdata
print(object_size(loadNamespace), humanReadable=TRUE)
print(bigObj, humanReadable=TRUE)
# utils
print(utils::object.size(loadNamespace), units="auto")
sapply(bigObj, utils:::format.object_size, units="auto")
# ll
ll("package:base")[order(-ll("package:base")$KB)[1:10],]

gdata documentation built on Oct. 22, 2024, 9:06 a.m.