23

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)

Mast
  • 1,788
  • 4
  • 29
  • 46
Cina
  • 9,759
  • 4
  • 20
  • 36
  • Isn't the memory allocation managed by the OS itself? –  Oct 31 '14 at 08:46
  • @Pascal, No R takes required memory and should release it after usage. but it doesn't for some reaseons i dont know ! – Cina Oct 31 '14 at 09:02
  • 1
    Did you quit or restart your R session? – Roland Oct 31 '14 at 09:06
  • I guess you also tried `gc(reset=T)` ? – Cath Oct 31 '14 at 09:08
  • 2
    @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 Oct 31 '14 at 09:34
  • @CathG, yes i tried. also tried `gc(verbose = T)`. – Cina Oct 31 '14 at 09:35
  • 1
    Are you using some of packages for parallel computing? – Jot eN Oct 31 '14 at 09:48
  • @JoteN , nop, i am using e1071 for classification but my dataset is huge. – Cina Oct 31 '14 at 11:03
  • 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. – Carl Witthoft Oct 31 '14 at 11:26

3 Answers3

11

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.

Cina
  • 9,759
  • 4
  • 20
  • 36
  • 3
    This seems to be the only option for me, too. It's rather annoying! – RobertMyles Aug 31 '16 at 17:49
  • 7
    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... – Davit Sargsyan Oct 23 '21 at 22:13
  • @DavitSargsyan: Do you know any update on that question? I am agree with you that gc() is not a proper solution (gives you only have the space back) – T. Beige Nov 24 '22 at 08:37
8

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.

Prabhu Shankar
  • 171
  • 1
  • 9
1

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))
Jot eN
  • 6,120
  • 4
  • 40
  • 59