title: saveRDS inflating size of object
body: <p>This is a tricky one as I can't provide a reproducible example, but I'm hoping that others may have had experience dealing with this.</p>
  
  <p>Essentially I have a function that pulls a large quantity of data from a DB, cleans and reduces the size and loops through some parameters to produce a series of lm model objects, parameter values and other reference values.  This is compiled into a complex list structure that totals about 10mb.</p>
  
  <p>It's then supposed to saved as an RDS file on AWS s3 where it's retrieved in a production environment to build predictions.</p>
  
  <p>e.g.</p>
  
  <pre><code>db.connection &lt;- db.connection.object
  
  
  build_model_list &lt;- function(db.connection) {   
  
  
  clean_and_build_models &lt;- function(db.connection, other.parameters) {
  
  
  get_db_data &lt;- function(db.connection, some.parameters) {# Retrieve db data} ## Externally defined
  
  db.data &lt;- get_db_data() 
  
  
  build_models &lt;- function(db.data, some.parameters) ## Externally defined
  
  clean_data &lt;- function(db.data, some.parameters) {# Cleans and filters data based on parameters} ## Externally defined
  
  
  clean.data &lt;- clean_data() 
  
  
  lm_model &lt;- function(clean.data) {# Builds lm model based on clean.data} ## Externally defined
  
  lm.model &lt;- lm_model()
  
  
  return(list(lm.model, other.parameters))} ## Externally defined
  
  
  looped.model.object &lt;- llply(some.parameters, clean_and_build_models)
  
  return(looped.model.object)}
  
  
  model.list &lt;- build_model_list()
  
  saveRDS(model.list, "~/a_place/model_list.RDS")
  </code></pre>
  
  <p>The issue I'm getting is that 'model.list' object which is only 10MB in memory will inflate to many GBs when I save locally as RDS or try to upload to AWS s3.</p>
  
  <p>I should note that though the function processes very large quantities of data (~ 5 million rows), the data used in the outputs is no larger than a few hundred rows.</p>
  
  <p>Reading the limited info on this on Stack Exchange, I've found that moving some of the externally defined functions (as part of a package) inside the main function (e.g. clean_data and lm_model) helps reduce the RDS save size.</p>
  
  <p>This however has some big disadvantages.  </p>
  
  <p>Firstly it's trial and error and follows no clear logical order, with frequent crashes and a couple of hours taken to build the list object, it's a very long debugging cycle.</p>
  
  <p>Secondly, it'll mean my main function will be many hundreds of lines long which will make future alterations and debugging much more tricky.</p>
  
  <p>My question to you is:</p>
  
  <p>Has anyone encountered this issue before?</p>
  
  <p>Any hypotheses as to what's causing it?</p>
  
  <p>Has anyone found a logical non-trial-and-error solution to this?</p>
  
  <p>Thanks for your help.</p>