I am periodically cleaning the memory in R using a call to rm(list=ls())
.
Do I need to call the garbage collector gc()
after that?
What is the difference between these 2 functions? Does gc()
call rm()
for certain variables?
I am periodically cleaning the memory in R using a call to rm(list=ls())
.
Do I need to call the garbage collector gc()
after that?
What is the difference between these 2 functions? Does gc()
call rm()
for certain variables?
Personally I like to include the
gc()
in loops to free up some RAM when the loops start filling up the available space. Something likeRe ThankGoat's comment on gc penalty, while this is true, one could of course decide to call gc every N iterations in a loop (where N can be parameterised in a number of ways). For loops where number of iterations is large, but resource usage within a given iteration is more modest, it may well not be necessary to do GC each and every iteration in order to regain desired performance.
Of course, if you're looping with a very large number of very high usage iterations, it's a different story, but at that stage it may well the case that the code simply needs to be vectorised and / or maybe even written in another language.
First, it is important to note that the two are very different in that
gc
does not delete any variables that you are still using- it only frees up the memory for ones that you no longer have access to (whether removed usingrm()
or, say, created in a function that has since returned). Runninggc()
will never make you lose variables.The question of whether you should call
gc()
after callingrm()
, though, is a good one. The documentation for gc helpfully notes:So the answer is that it can be good to call
gc()
(and at the very least, can't hurt), even though it would likely be triggered anyway (if not right away, then soon).