How do I clean up R memory without restarting my PC?

I am running my code in R (under Windows) which involves a lot of in-memory data. I tried to use rm(list=ls()) to clean up memory, but seems the memory is still occupied and I cannot rerun my code. I tried to close the R and restart R again, but it is the same, seems memory is still occupied as when I run the codes it says it can't allocate memory (but it could at the first time). The memory seems only cleaned up after I restart my PC.

Is there any way to clean up the memory so that I can rerun my code without restarting my PC every time?


Solution 1:

Maybe you can try to use the function gc(). A call of gc() causes a garbage collection to take place. 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. gc() also return a summary of the occupy memory.

Solution 2:

I came under the same problem with R. I dig a bit and come with a solution, that we need to restart R session to fully clean the memory/RAM. For this, you can use a simple code after removing everything from your workspace. the code is as follows :

rm(list = ls())

.rs.restartR()