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?