简体   繁体   English

即使设置了 rm(df),R 循环也会消耗 5GB 的 RAM

[英]R loop consumes 5GB of RAM even when rm(df) is set

I've a loop to make an API call and write a CSV with the data.我有一个循环来进行 API 调用并用数据编写一个 CSV 文件。

After writting it to Local Disk, I've set rm(df).将其写入本地磁盘后,我已经设置了 rm(df)。 It starts cool, but after 10 or more calls the RAM, and the PC in general starts to slow down very hard.它开始很酷,但在 10 次或更多次调用 RAM 后,PC 通常开始非常努力地减速。

Windows task manager says RStudio is using 5GB of RAM (total of my PC is 8RM). Windows 任务管理器说 RStudio 正在使用 5GB 的 RAM(我的 PC 总共是 8RM)。

Why is this happening if I'm removing the df after it is written to local disk?如果我在将 df 写入本地磁盘后删除它,为什么会发生这种情况?

library(googleAuthR)
library(googleAnalyticsR)
library(tidyverse)

my_dates <- seq(as.Date("2019-10-25"), as.Date("2019-12-31"), by = 1)


my_fetch <- function(ga_id, d) {



       df <- google_analytics(ga_id,
                   date_range = c(d, d),
                   metrics = c("totalEvents"),
                   dimensions = c("ga:date", "ga:eventCategory", 
                   "ga:eventAction", "ga:eventLabel"),
                   anti_sample = TRUE)



      filename <- paste0("tvgo-web", "-", d, ".csv")
      print(filename)
      write.csv(df, filename, row.names = FALSE)
      rm(df)
      gc()

}



my_fetches_fetches <- mapply(my_fetch, myviewID, my_dates, SIMPLIFY = FALSE)

Each API call as a CSV is 12 MB.每个 CSV 格式的 API 调用为 12 MB。 It has:它有:

dim(df)
[1] 135410      5

正如@G5W 在评论中发布的那样, rm(df)之后的gc() rm(df)将释放内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM