Error: cannot allocate vector of size X Mb in R

泪湿孤枕 提交于 2020-01-23 13:25:11

问题


I have question regarding memory usage in R. I am running a Rcode on our entire database in R in a for loop. However, the code stops at some point saying that it cannot allocate a vector of size 325.7 mb. When I was looking at the task manager I saw that it was using 28gb of RAM on our server.

I am familiar with the gc() function in R but this does not seems to work. E.g. the code stopped working on the 15th iteration, saying that it cannot allocate the vector. However, if I only run the 15th iteration (and nothing else) there is no problem at all. Moreover, for each new iteration I delete my DT which is by far the largest object in my environment.

Code sample:

DT <- data.table() 
items <- as.character(seq(1:10)) 
for (i in items){ 
  DT <- sample(x = 5000,replace = T) 
  write.csv(DT,paste0(i,".csv")) 
  gc() 
  rm(DT) 
} 

I have the feeling that this gc function does not work properly in a for loop. Is that correct or are there any other possible issues, i.e. are there reasons why my memory is full after a few iterations?

来源:https://stackoverflow.com/questions/31781095/error-cannot-allocate-vector-of-size-x-mb-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!