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
How Calculate Growth Rate in Long Format Data Frame
R, Deep VS. Shallow Copies, Pass by Reference
Evaluate Inline R Code in Rmarkdown Figure Caption
How to Read Knitr/Rmd Cache in Interactive Session
Sine Curve Fit Using Lm and Nls in R
How to Show the Progress of Code in R
Data.Table Error When Used Through Knitr, Gwidgetswww
Sum Multiple Columns by Group with Tapply
Remove Empty Factors from Clustered Bargraph in Ggplot2 with Multiple Facets
Insert Images Using Knitr::Include_Graphics in a for Loop
Faster Way to Compare Rows in a Data Frame
Different Font Faces and Sizes Within Label Text Entries in Ggplot2
Clustered Standard Errors in R Using Plm (With Fixed Effects)
Warning When Defining Factor: Duplicated Levels in Factors Are Deprecated
Ggplot2: How to Adjust Fill Colour in a Boxplot (And Change Legend Text)
How to Extract Data from a Rasterbrick
How to Change Strip.Text Labels in Ggplot with Facet and Margin=True