jeanmanguy
4/26/2018 - 12:16 PM

list all objects and get the allocated space for each object, in a sorted data_frame

list all objects and get the allocated space for each object, in a sorted data_frame

library(purrr)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union


# some objects
x <- 1:10
y <- rep("string", 10)
z <- data_frame(a = rnorm(5000), b = rnorm(5000, 1, 2), c = rnorm(5000, 0, 2))

# list all objects and get the allocated space for each object
ls() %>%
  data_frame(object = .) %>%
  mutate(
    size = map(object, ~ object.size(get(.x))),
    size_bytes = as.integer(size),
    size_formatted = map_chr(size, ~ format(.x, units = "auto"))
  ) %>%
  arrange(desc(size_bytes)) %>%
  select(-size)
#> # A tibble: 3 x 3
#>   object size_bytes size_formatted
#>   <chr>       <int> <chr>         
#> 1 z          121016 118.2 Kb      
#> 2 y             216 216 bytes     
#> 3 x              88 88 bytes

Session info

devtools::session_info()
#> Session info -------------------------------------------------------------
#>  setting  value                       
#>  version  R version 3.4.4 (2018-03-15)
#>  system   x86_64, linux-gnu           
#>  ui       X11                         
#>  language en_IE:en                    
#>  collate  en_IE.UTF-8                 
#>  tz       Europe/Dublin               
#>  date     2018-04-26
#> Packages -----------------------------------------------------------------
#>  package     * version    date       source                            
#>  ansistrings   1.0.0.9000 2018-04-16 Github (r-lib/ansistrings@4e4d309)
#>  assertthat    0.2.0      2017-04-11 CRAN (R 3.4.2)                    
#>  backports     1.1.2      2017-12-13 CRAN (R 3.4.3)                    
#>  base        * 3.4.4      2018-03-16 local                             
#>  bindr         0.1.1      2018-03-13 CRAN (R 3.4.3)                    
#>  bindrcpp    * 0.2.2      2018-03-29 CRAN (R 3.4.4)                    
#>  cli           1.0.0.9002 2018-04-16 Github (r-lib/cli@c408924)        
#>  compiler      3.4.4      2018-03-16 local                             
#>  crayon        1.3.4      2018-02-10 Github (r-lib/crayon@95b3eae)     
#>  datasets    * 3.4.4      2018-03-16 local                             
#>  devtools      1.13.5     2018-02-18 CRAN (R 3.4.3)                    
#>  digest        0.6.15     2018-01-28 CRAN (R 3.4.3)                    
#>  dplyr       * 0.7.4      2017-09-28 CRAN (R 3.4.2)                    
#>  evaluate      0.10.1     2017-06-24 CRAN (R 3.4.2)                    
#>  glue          1.2.0      2017-10-29 CRAN (R 3.4.2)                    
#>  graphics    * 3.4.4      2018-03-16 local                             
#>  grDevices   * 3.4.4      2018-03-16 local                             
#>  hms           0.4.2      2018-03-10 CRAN (R 3.4.3)                    
#>  htmltools     0.3.6      2017-04-28 CRAN (R 3.4.2)                    
#>  knitr         1.20       2018-02-20 CRAN (R 3.4.3)                    
#>  magrittr      1.5        2014-11-22 cran (@1.5)                       
#>  memoise       1.1.0      2017-04-21 CRAN (R 3.4.2)                    
#>  methods     * 3.4.4      2018-03-16 local                             
#>  pillar        1.2.1      2018-02-27 CRAN (R 3.4.3)                    
#>  pkgconfig     2.0.1      2017-03-21 CRAN (R 3.4.2)                    
#>  prettyunits   1.0.2      2015-07-13 CRAN (R 3.4.2)                    
#>  progress      1.1.2.9003 2018-04-16 Github (r-lib/progress@e525de4)   
#>  purrr       * 0.2.4      2017-10-18 CRAN (R 3.4.2)                    
#>  R6            2.2.2      2017-06-17 CRAN (R 3.4.2)                    
#>  Rcpp          0.12.16    2018-03-13 CRAN (R 3.4.3)                    
#>  rlang         0.2.0.9001 2018-04-16 Github (tidyverse/rlang@82b2727)  
#>  rmarkdown     1.9        2018-03-01 CRAN (R 3.4.3)                    
#>  rprojroot     1.3-2      2018-01-03 CRAN (R 3.4.3)                    
#>  selectr       0.4-1      2018-04-06 CRAN (R 3.4.4)                    
#>  stats       * 3.4.4      2018-03-16 local                             
#>  stringi       1.1.7      2018-03-12 CRAN (R 3.4.3)                    
#>  stringr       1.3.0      2018-02-19 CRAN (R 3.4.3)                    
#>  tibble        1.4.2      2018-01-22 CRAN (R 3.4.3)                    
#>  tools         3.4.4      2018-03-16 local                             
#>  utf8          1.1.3      2018-01-03 CRAN (R 3.4.3)                    
#>  utils       * 3.4.4      2018-03-16 local                             
#>  withr         2.1.2      2018-03-28 Github (jimhester/withr@79d7b0d)  
#>  xml2          1.2.0      2018-01-24 CRAN (R 3.4.3)                    
#>  yaml          2.1.18     2018-03-08 CRAN (R 3.4.3)