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.

Value

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

Note

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 object_size function in the gdata package used to be called object.size, masking the base R function of the same name. This was a potential source of ambiguity and errors when running scripts, depending on whether the gdata package was loaded or not. This was improved in the 3.0.0 release of gdata by renaming the function to object_size and in version 3.1.0 when object.size was removed from the gdata package.

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],]

warnes/gdata documentation built on Jan. 6, 2025, 6:19 p.m.