0

I was hoping to make a global function that would clear my workspace and dump my memory. I called my function "cleaner" and want it to execute the following code:

remove(list = ls())
gc()

I tried making the function in the global environment but when I run it, the console just prints the text of the function. In my function file to be sourced:

cleaner <- function(){
    remove(list = ls())
    gc()
    #I tried adding return(invisible()) to see if that helped but no luck
}

cleaner()

Even when I make the function in the script I want it to run (cutting out any potential errors with sourcing), the storage dump seems to work, but it still doesn't clear the workspace.

enter image description here

Phil
  • 7,287
  • 3
  • 36
  • 66
JMoney
  • 3
  • 2
  • cleaner()? Or cleanerFile()? – Phil Jul 14 '20 at 18:16
  • Oops, I attached the wrong screenshot. Same result though. – JMoney Jul 14 '20 at 18:19
  • 1
    Never mind my previous comment, it doesn't do what you want. The best way to do what you want is to restart R, but I don't know of a good way to run a command line that does it. In RStudio, you would use the shortcut Ctrl (or Cmd) + Shift + F10, or click on Session -> Restart R – Phil Jul 14 '20 at 18:25
  • When I do the Ctrl + Shift + F10 and it restarts I still have all of my variables, df, functions etc in my environment. Googling this shows that's due to auto-saving my .RData, which I would rather not disable. I have a keyboard shortcut for clearing workspace, but I wondered if it was possible to make one command that also does gc() since there is no keyboard shortcut for that. It's two lines of code so not too much work at all, but now I am just wondering why my function above doesn't work. – JMoney Jul 14 '20 at 18:37
  • Likely because you are saving .RData, which I recommend you don't do. https://onunicornsandgenes.blog/2017/04/02/using-r-dont-save-your-workspace/ – Phil Jul 14 '20 at 18:45

1 Answers1

1

Two thoughts about this: Your code does not delete all objects, to also remove the hidden ones use

rm(list = ls(all.names = TRUE))

There is also the command gctorture() which provokes garbage collection on (nearly) every memory allocation (as the man page said). It's intended for R developers to ferret out memory protection bugs:

cleaner <- function(){
    # Turn it on
    gctorture(TRUE)

    # Clear workspace
    rm(list = ls(all.names = TRUE, envir=sys.frame(-1)),
       envir = sys.frame(-1))

    # Turn it off (important or it gets very slow)
    gctorture(FALSE)
}

If this procedure is used within a function, there is the following problem: Since the function has its own stack frame, only the objects within this stack frame are deleted. They still exist outside. Therefore, it must be specified separately with sys.frame(-1) that only the higher-level stack frame should be considered. The variables are then only deleted within the function that calls cleaner() and in cleaner itself when the function is exited.

But this also means that the function may only be called from the top level in order to function correctly (you can use sys.frames() which lists all higher-level stack frames to build something that also avoids this problem if really necessary)

fcdt
  • 2,371
  • 5
  • 14
  • 26
  • Thank you, I didn't know about the hidden files. Your code works great and I can run it as 3 lines, but is there really no way to make a function like cleaner <- function (){ your 3 lines from above } then call cleaner()? – JMoney Jul 14 '20 at 20:01