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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
First, it is important to note that the two are very different in that
gcdoes 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).