[R] Object size in bytes

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Sep 6 13:42:10 CEST 2000

> From: Martin Maechler <maechler at stat.math.ethz.ch>
> Date: Wed, 6 Sep 2000 12:48:07 +0200 (CEST)
> >>>>> "Duncan" == Duncan Murdoch <murdoch at stats.uwo.ca> writes:
>     Duncan> On Sat, 2 Sep 2000 10:38:09 +0200 (MET DST),Kjetil Kjernsmo
>     Duncan> <kjetil.kjernsmo at astro.uio.no> wrote in message
>     Duncan> <Pine.OSF.4.05.10009021034320.15448-100000 at alnair.uio.no>:
>     >> Dear all,
>     >> 
>     >> If I may request a feature, I would find it very useful to be able
>     >> to obtain a the sizes of the objects along with object names with
>     >> e.g. ls().
>     Duncan> In S-PLUS, objects.summary() does this. (Size means number of
>     Duncan> components, not size in bytes, so it's not very informative for
>     Duncan> some structured objects, but it definitely does let you find
>     Duncan> the big vectors of numbers.)  Kjetil's right, it's useful.
> just the number of components...  should be "easy" (well..)
> writing a function that recursively sums up length() of atomic components..
> would that really be okay?

That's *not* what S-PLUS does now (if it ever did). From ?objects.summary:

          a numeric vector giving the object sizes in bytes, as defined
          by function object.size.

and object.size() gives the (potentially) allocated memory, including overheads.

If you just want the number of components, try

objects.length <- function(list)
   res <- sapply(list, function(x) length(get(x)))
   names(res) <- list

One possible catch is duplicated strings:

zz <- rep("a long string", 10000)

is not actually the size which object.size() reports (180041), and I think
it is actually about 40000 bytes in R, looking at gc().

Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list