Automatic Garbage collection on R Studio Server

I would like to know how to run garbage collection automatically in a specific time interval or at a specific memory limit.

For example, I would like to clear the garbage for every hour or whenever it reaches 5 GB limit

Any help would be much appreciated. Thanks!

1 Like

Garbage collection is a function of R rather than RStudio. R only periodically does implicit garbage collection, and doesn't routinely release memory that it has asked the operating system to allocate. If you want/need to do this explicitly, see


However, Hadley Wickham has this advice in Advanced R:

Despite what you might have read elsewhere, there's never any need to call gc() yourself. R will automatically run garbage collection whenever it needs more space; if you want to see when that is, call gcinfo(TRUE). The only reason you might want to call gc() is to ask R to return memory to the operating system. However, even that might not have any effect: older versions of Windows had no way for a program to return memory to the OS.

GC takes care of releasing objects that are no longer used. However, you do need to be aware of possible memory leaks.

1 Like