lsize: Report objects and their memory sizes

lsizeR Documentation

Report objects and their memory sizes

Description

lsize is like ls, except it returns a numeric vector whose names are the object names, and whose elements are the object sizes. The vector is sorted in order of increasing size. lsize avoids loading objects cached using mlazy; instead of their true size, it uses the size of the file that stores each cached object, which is shown as a negative number. The file size is typically smaller than the size of the loaded object, because mlazy saves a compressed version. NB that lsize will scan all objects in the environment, including ones with funny names, whereas ls does so only if its all.names argument is set to TRUE.

Missing objects should return 0 (which may or may not be exactly correct!). You won't normally get that, but see Examples for a perverse case.

Environments

If there are environment-objects (which are really symbols that point to frames— see R) within the very environment you are lsizeing, what should their size be? There is no perfect answer. R will tell you it's "56 bytes" and that's what you'll get with the default recursive=0. However, each environment could be holding arbitrarily large objects— so you might want to know how much memory they are "really" taking. You can do so by setting recursive to a positive number, which also controls the depth of recursion (because environments can themselves contain other environments).

However-however, those environments might be innocuous things that just refer to shared system-y ones (eg namespaces of packages, copies of .GlobalEnv, etc), in which case they are not costing any memory. And if two symbols refer to the same actual environment, they are duplicates and second one is not taking any extra real memory. So lsize tries to keep track of such cases (whenever recursive>0), and not to incorporate their memory-use; whether it does so optimally, is another Q. NB that for duplicates, only the alphabetically-first will be recursed.

There are lots of places that environments can lurk within other objects: notably, environments-of-functions, and formulae/results of calls to lm etc. These can take huge amounts of memory, sometimes manifest only when saving/loading. lsize does not currently attempt to measure those; but see find.lurking.envs.

Usage

lsize( envir=.GlobalEnv, recursive=0)

Arguments

envir

where to look for the objects. Will be coerced to environment, so that e.g. lsize( 2) and lsize( "package:mvbutils") work. envir can be a sys.frame– useful during debugging.

recursive

depth of recursion to allow, for objects that are themselves environments. See .ENVIRONMENTS.

Value

Named numeric vector.

Author(s)

Mark Bravington

See Also

ls, mlazy, find.lurking.envs

Examples

# Current workspace
lsize()
# Contrived example to show objects in a function's environment
nonsense <- function(..., a, b, c) lsize( environment())
try( # this might be fragile with missings; OK in R4.3
  nonsense()
)
# a, b, c are all missing; this example might break in future R versions
# ...   a   b   c
#   0   0   0   0

mvbutils documentation built on May 25, 2026, 5:09 p.m.

Related to lsize in mvbutils...