This function attempts to estimate the real object size of an object. If the object has pass-by-reference semantics, it may not estimate the object size well without a specific method developed. For the case of terra class objects, this will be accurate (both RAM and file size), but only if it is not passed inside a list or environment. To get an accurate size of these, they should be passed individually.

objSize(x, quick = FALSE, ...)

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



An object


Logical. If FALSE, then an attribute, "objSize" will be added to the returned value, with each of the elements' object size returned also.


Additional arguments (currently unused), enables backwards compatible use.


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.


Logical indicating whether to include enclosing environments. Default TRUE.


For internal account keeping to identify and prevent duplicate counting


This will return the result from lobstr::obj_size, i.e., a lobstr_bytes

which is a numeric. If quick = FALSE, it will also have an attribute, "objSize", which will be a list with each element being the objSize of the individual elements of x. This is particularly useful if x is a list or environment. However, because of the potential for shared memory, the sum of the individual elements will generally not equal the value returned from this function.


For functions, a user can include the enclosing environment as described and 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.



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

objSize(foo) # all the elements in the environment
#> 2.85 kB
utils::object.size(foo) # different - only measuring the environment as an object
#> 56 bytes

utils::object.size(prepInputs) # only the function, without its enclosing environment
#> 143664 bytes
objSize(prepInputs)     # the function, plus its enclosing environment
#> 37.56 kB

os1 <- utils::object.size(as.environment("package:reproducible"))
(os1) # very small -- just the environment container
#> 568 bytes

# slow next bit
# \donttest{
os2 <- objSize(as.environment("package:reproducible"))
sum(unlist(os2)) # possibly 100+ MB, with all functions, objects
#> [1] 50766328
                 # and imported functions
# }