0

I'm working with some large objects in R and going out of memory is a concern. I want to save on memory by defining them once as global variables, rather than passing them as arguments to functions where they'd be defined twice, once outside the function and once as an argument.

Will this really save memory? If so, is there any "weird" behaviour using global variables this way that I should be aware of?

R Greg Stacey
  • 425
  • 4
  • 15
  • Usually a large dataframe. And yes, the functions usually do some transformation to the dataframe. I think I can do that from inside a function with <<- or assign(). – R Greg Stacey Aug 11 '21 at 21:27
  • @akrun Can you post an example? I'm confused how/if to use `:=` for managing memory. – R Greg Stacey Aug 11 '21 at 21:31
  • 2
    This won't save on memory. The value isn't copied when passed to a function. The value will only be copied if you try to edit the value in the function. R uses [copy-on-modify semantics](https://stackoverflow.com/questions/15759117/what-exactly-is-copy-on-modify-semantics-in-r-and-where-is-the-canonical-source). – MrFlick Aug 11 '21 at 21:43
  • @MrFlick The functions modify the large objects, and in which case they'll be copied, right? Can I prevent the large objects from being copied by referring to them as global variables? – R Greg Stacey Aug 11 '21 at 21:52
  • 1
    Well, if you are modifying objects, a copy is going to happen somewhere regardless. At least that's how base R objects work. Just make sure you don't keep a reference to the old value so it can be garbage collected. You could use something like the data.table library if you want to modify by reference. But then it's still not necessary to keep things in the global environment. – MrFlick Aug 11 '21 at 21:55
  • @MrFlick Wonderful, thanks for clarifying. I see this link https://cran.r-project.org/web/packages/data.table/vignettes/datatable-reference-semantics.html and it looks like `:=` is what I'm looking for. – R Greg Stacey Aug 11 '21 at 22:01

0 Answers0