Skip to content

Instantly share code, notes, and snippets.

@DavisVaughan
Created May 31, 2019 19:38
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DavisVaughan/669a71979dc6a913d44f6906479d1eca to your computer and use it in GitHub Desktop.
Save DavisVaughan/669a71979dc6a913d44f6906479d1eca to your computer and use it in GitHub Desktop.
Exploring the object size of `lm()` objects with enclosing environments
library(purrr)
library(lobstr)
library(glue)
library(rlang, warn.conflicts = FALSE)
make_an_lm <- function() {
x <- rep(1L, times = 10000000)
cat(glue("x is {object.size(x)}B"))
lm(1 ~ 1)
}
lm_model <- make_an_lm()
#> x is 40000048B
# that's not so bad...
object.size(lm_model)
#> 11336 bytes
# wtf?
obj_size(lm_model)
#> 40,008,504 B
# lm_model is a list...so
str(map(lm_model, obj_size), digits.d = 10)
#> List of 11
#> $ coefficients : 'lobstr_bytes' num 368
#> $ residuals : 'lobstr_bytes' num 280
#> $ effects : 'lobstr_bytes' num 368
#> $ rank : 'lobstr_bytes' num 56
#> $ fitted.values: 'lobstr_bytes' num 280
#> $ assign : 'lobstr_bytes' num 56
#> $ qr : 'lobstr_bytes' num 2096
#> $ df.residual : 'lobstr_bytes' num 56
#> $ call : 'lobstr_bytes' num 560
#> $ terms : 'lobstr_bytes' num 40002832
#> $ model : 'lobstr_bytes' num 40003576
# terms? model?
str(lm_model$terms)
#> Classes 'terms', 'formula' language 1 ~ 1
#> ..- attr(*, "variables")= language list(1)
#> ..- attr(*, "factors")= int(0)
#> ..- attr(*, "term.labels")= chr(0)
#> ..- attr(*, "order")= int(0)
#> ..- attr(*, "intercept")= int 1
#> ..- attr(*, "response")= int 1
#> ..- attr(*, ".Environment")=<environment: 0x7ff86949f448>
#> ..- attr(*, "predvars")= language list(1)
#> ..- attr(*, "dataClasses")= Named chr "numeric"
#> .. ..- attr(*, "names")= chr "1"
# attributes?
str(map(attributes(lm_model$terms), obj_size), digits.d = 10)
#> List of 10
#> $ variables : 'lobstr_bytes' num 224
#> $ factors : 'lobstr_bytes' num 48
#> $ term.labels : 'lobstr_bytes' num 48
#> $ order : 'lobstr_bytes' num 48
#> $ intercept : 'lobstr_bytes' num 56
#> $ response : 'lobstr_bytes' num 56
#> $ class : 'lobstr_bytes' num 176
#> $ .Environment: 'lobstr_bytes' num 40000216
#> $ predvars : 'lobstr_bytes' num 224
#> $ dataClasses : 'lobstr_bytes' num 336
# environment..........
lm_model_env <- attr(lm_model$terms, ".Environment")
env_names(lm_model_env)
#> [1] "x"
# `x` was carried along!
obj_size(lm_model_env$x)
#> 40,000,048 B
# saves to 40+Mb, or 20+Mb with compression when it "should" only be 0.01 Mb
# (would be 80Mb, but `x` is only saved once, I think)
# saveRDS(lm_model, "~/Desktop/lm.rds", compress = FALSE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment