How to clean up R memory (without the need to restart my PC)?

后端 未结 8 916
小蘑菇
小蘑菇 2020-12-07 14:37

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

8条回答
  •  执笔经年
    2020-12-07 15:10

    memory.size(max=T) # gives the amount of memory obtained by the OS
    [1] 1800
    memory.size(max=F) # gives the amount of memory being used
    [1] 261.17
    

    Using Paul's example,

    m = matrix(runif(10e7), 10000, 1000)
    

    Now

    memory.size(max=F)
    [1] 1024.18
    

    To clear up the memory

    gc()
    memory.size(max=F)
    [1] 184.86
    

    In other words, the memory should now be clear again. If you loop a code, it is a good idea to add a gc() as the last line of your loop, so that the memory is cleared up before starting the next iteration.

提交回复
热议问题