【问题标题】:Out of memory when writing inside of function but not outside (R)在函数内部而不是外部写入时内存不足(R)
【发布时间】:2017-01-18 15:57:44
【问题描述】:

我有每月报告文件,每个文件大约 70MB。我编写了以下函数将它们合并到一个文件中:

fusionfil<-function(ruta){
  require(data.table)
  fusionfrag<-paste0(ruta,grep("_filt",dir(ruta),value = TRUE))

  datalist<-list()

  for(frag in fusionfrag){
    datalist[[frag]] <- fread(frag,colClasses="character")
  }

  datalist<-do.call(rbind,datalist)

  fwrite(datalist,"fusion.csv")
}

但是会导致如下错误:

Warning messages:
1: In rbindlist(l, use.names, fill, idcol) :
  Reached total allocation of 8082Mb: see help(memory.size)
2: In rbindlist(l, use.names, fill, idcol) :
  Reached total allocation of 8082Mb: see help(memory.size)

但是,如果我取出最终的 fwrite 并将函数的输出分配给一个变量,那么我可以毫无问题地编写它。我想了解这里发生了什么(如果有更有效的方法来实现我想要做的事情,那就太好了)。

【问题讨论】:

    标签: r memory data.table


    【解决方案1】:

    尝试清理一些内存

    gc()
    

    在调用fwrite之前达到同样的效果。

    更新:我错过了列表和组合表具有相同的名称。

    【讨论】:

    • 如果我rm datalist 那么fwrite 怎么知道要写什么?我尝试了与datalist &lt;- do.call(rbind,datalist) 行类似的操作,据我了解,它应该用合并的对象替换不再相关的 data.tables 列表。
    • 好的,只需添加 gc() 即可解决问题。谢谢!
    • 我错过了列表和组合表具有相同的名称。
    猜你喜欢
    • 2018-12-25
    • 2021-07-12
    • 2015-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-08
    相关资源
    最近更新 更多