clear memory allocated by R session (gc() doesnt help !)

35,892

Solution 1

best solution i found is restarting R session. in R studio ctr+shft+f10

and if you dont want to save workspace

makeActiveBinding("refresh", function() { system(paste0(R.home(),"/bin/i386/R")); q("no") }, .GlobalEnv)

paste0(R.home(),"/bin/i386/R --no-save") #--save will save workspace

cheers.

Solution 2

you need to follow two steps. First, run rm(list = ls()) However, though it removes all the objects in ls(), you need to restart R using .rs.restartR()

This will effectively clear the memory completely.

Solution 3

As in this answer - https://stackoverflow.com/a/8813862/2602477 - "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".

You could remove (almost) everything in the working environment using rm function:

rm(list = ls())

Note that if you want to remove hidden objects as well you need to use

rm(list = ls(all.names = TRUE))
Share:
35,892

Related videos on Youtube

Cina
Author by

Cina

Updated on August 06, 2020

Comments

  • Cina
    Cina over 3 years

    I am doing machine learning in large scale but after while my compute getting so slow because of R memory occupation.
    I cleared my objects and also I tried gc() and the result:

               used  (Mb) gc trigger   (Mb)  max used   (Mb)
    Ncells  4460452 231.5   15288838 1116.6  36599071 1954.7
    Vcells 29572776 349.4  324509788 2712.9 350796378 3376.4
    

    My task manager shows R session still allocated over 3GB of my memory and my computer is still slow.
    How can I release the memory taken by R? (I don't want restart the pc)

    • Admin
      Admin over 9 years
      Isn't the memory allocation managed by the OS itself?
    • Cina
      Cina over 9 years
      @Pascal, No R takes required memory and should release it after usage. but it doesn't for some reaseons i dont know !
    • Roland
      Roland over 9 years
      Did you quit or restart your R session?
    • Cath
      Cath over 9 years
      I guess you also tried gc(reset=T) ?
    • Cina
      Cina over 9 years
      @Roland, i suppose it will be solved if i restart session. but actually i dont want to do that because i face this problem quite often and dont want to restart session every time.
    • Cina
      Cina over 9 years
      @CathG, yes i tried. also tried gc(verbose = T).
    • Jot eN
      Jot eN over 9 years
      Are you using some of packages for parallel computing?
    • Cina
      Cina over 9 years
      @JoteN , nop, i am using e1071 for classification but my dataset is huge.
    • Carl Witthoft
      Carl Witthoft over 9 years
      I've seen similar apparent memory leaks; sometimes it appears to be a matter of leaving graphics windows open or having many libraries loaded. However, that's a subjective comment, as I haven't rigorously checked it out.
  • Cina
    Cina over 9 years
    already tried. no big change
  • RobertMyles
    RobertMyles over 7 years
    This seems to be the only option for me, too. It's rather annoying!
  • jangorecki
    jangorecki about 6 years
    Error in .rs.restartR() : could not find function ".rs.restartR"
  • Shique
    Shique over 5 years
    .rs.restartR() is a RStudio method, it does not work with regular R
  • Davit Sargsyan
    Davit Sargsyan over 2 years
    This is not the answer to the question. Neither is any other answer that suggest session restart. Of course, if you restart the session everything will be removed. But you shouldn't have to do it. Once an object is removed and garbage collection done using gc(), the memory should be freed. For some reason R doesn't do it properly. This is an important issue and should be addressed by R Core team. It's been bugging me for years...