Object.Size() Reports Smaller Size Than .Rdata File

object.size() reports smaller size than .Rdata file

One way this can happen is if the object has an associated environment that needs saving with it if it is to make sense. This comes up most commonly in the context of "closures" (see here for one explanation).

Without a reproducible example (and without having used R2jags myself) I can't tell you whether that's what is going on in your case, but it at least seems plausible, given that: (a) closures seem to be the most common cause of this situation; (b) based on the output of str(out), your object seems to include a bunch of functions; and (c) it seems like this might be a useful way to organize a computation-heavy and possibly parallelizable procedure like MCMC.

## Define a function "f" that returns a closure, here assigned to the object "y"
f <- function() {
x <- 1:1e6
function() 2*x
}
y <- f()
environment(y)
# <environment: 0x0000000008409ab8>

object.size(y)
# 1216 bytes

save(y, file="out.Rdata")
file.info("out.Rdata")$size
# [1] 2128554

Object size discrepancy

If you save using either the save or saveRDS commands, the default is to use compression. If you have different content in the vectors, they'll compress differently...

Try save with compress=FALSE and compare again...

In the example below there is almost a 700x difference in file size:

set.seed(42)
x <- runif(1e6) # random values should not compress well...
y <- rep(0, 1e6) # zeroes should compress very well...
object.size(x) # 8000040 bytes
object.size(y) # 8000040 bytes

save('x', file='x.rds')
save('y', file='y.rds')
file.info(c('x.rds', 'y.rds'))$size
#[1] 5316773 7838

save('x', file='x.rds', compress=FALSE)
save('y', file='y.rds', compress=FALSE)
file.info(c('x.rds', 'y.rds'))$size
#[1] 8000048 8000048

Object size discrepancy

If you save using either the save or saveRDS commands, the default is to use compression. If you have different content in the vectors, they'll compress differently...

Try save with compress=FALSE and compare again...

In the example below there is almost a 700x difference in file size:

set.seed(42)
x <- runif(1e6) # random values should not compress well...
y <- rep(0, 1e6) # zeroes should compress very well...
object.size(x) # 8000040 bytes
object.size(y) # 8000040 bytes

save('x', file='x.rds')
save('y', file='y.rds')
file.info(c('x.rds', 'y.rds'))$size
#[1] 5316773 7838

save('x', file='x.rds', compress=FALSE)
save('y', file='y.rds', compress=FALSE)
file.info(c('x.rds', 'y.rds'))$size
#[1] 8000048 8000048

Why do objects in RStudio/R which display as 1 GB in size get saved by RData or RDS file formats at much larger sizes, even at no compression?

The calculation of the size of objects in R is complicated by the need for efficient memory management. Your list may contain elements that are not accounted for while in memory as they may be shared resources, but will need to be included when exported. The help file for object.size states that:

Exactly which parts of the memory allocation should be attributed to
which object is not clear-cut. This function merely provides a rough
indication: it should be reasonably accurate for atomic vectors, but
does not detect if elements of a list are shared, for example.
(Sharing amongst elements of a character vector is taken into account,
but not that between character vectors in a single object.)



Related Topics



Leave a reply



Submit