R foreach 循环中的内存溢出不需要在内存中存储任何内容

问题描述

我正在 R 中运行这段简单的代码,使用 foreach 为每个进程写入同一个文件。每次迭代向每个文件附加一行。每次迭代不存储变量,但内存使用量不断增加,直到溢出。怎么了?

  cores <- parallel::detectCores() - 1
  cl <- parallel::makeCluster(cores,outfile = "")
  doParallel::registerDoParallel(cl)  

  foreach::foreach(x = 1:length(file.paths)) %dopar% {
    
    my.file <- file.paths[x]
    load(my.file)
    # List my.list is loaded
    
    if (!my.list$is_0) {
      
      # loop entry condition is met >99.99% of the time
      my.df <- compute.values(my.list)
      # my.df is a 1-row data frame

      # one unique output file per process
      o_file <- paste0("out-",Sys.getpid(),".csv")

      # The row is appended to the output file
      data.table::fwrite(
        x         = my.df,file      = o_file,sep       = ",",na        = "",quote     = FALSE,append    = TRUE,col.names = FALSE,row.names = FALSE
      )
    }
  }
  
  # Stop parallelization
  parallel::stopCluster(cl)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)