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 using rm()
or, say, created in a function that has since returned). Running gc()
will never make you lose variables.
The question of whether you should call gc()
after calling rm()
, though, is a good one. The documentation for gc helpfully notes:
A call of gc causes a garbage collection to take place. This will also take place automatically without user intervention, and the primary purpose of calling gc is for the report on memory usage.
However, it can be useful to call gc after a large object has been removed, as this may prompt R to return memory to the operating system.
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).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…