我需要删除一个全局变量,并使用R从函数内部释放它所使用的内存,但我尝试过的这些选项都不起作用。
我用envir参数和gc尝试过rm函数,但是gc没有释放内存。我还尝试过在全局环境中使用eval+envir运行gc。
library(data.table)
DT = data.table(col1 = 1:1e6)
cols = paste0('col', 2:100)
for (col in cols){ DT[, col := 1:1e6, with = F] }
rm_and_release <- function(dt){
dt <- dt[sample(1e6, 9e5, FALSE)]
print(gc())
rm(DT, envir = globalenv())
print(gc())
}
rm_and_release(DT)结果如下所示
used (Mb) gc trigger (Mb) max used (Mb)
Ncells 661556 35.4 1168576 62.5 1143443 61.1
Vcells 96303112 734.8 146725516 1119.5 146722586 1119.5
used (Mb) gc trigger (Mb) max used (Mb)
Ncells 661569 35.4 1168576 62.5 1143443 61.1
Vcells 96303114 734.8 146725516 1119.5 146722586 1119.5我希望第二个gc()能释放更多的内存,因为那时只有一个数据集,因为全局DT已被删除。
我需要释放函数内部的RAM,因为函数会生成更多的数据集,并且会耗尽内存。
发布于 2019-05-29 22:08:04
像这样使用rm的list=参数:
library(data.table)
DT = data.table(col1 = 1:1e6)
cols = paste0('col', 2:100)
for (col in cols){ DT[, col := 1:1e6, with = F] }
rm_and_release <- function(dt){
dt <- dt[sample(1e6, 9e5, FALSE)]
print(gc())
rm(list = "DT", envir = globalenv())
print(gc())
}
rm_and_release(DT)
exists("DT")
## [1] FALSE备注
下面是我(在Windows上)运行它时的日志:
> library(data.table)
> gc()
used (Mb) gc trigger (Mb) max used (Mb)
Ncells 1075660 57.5 1899034 101.5 1899034 101.5
Vcells 2609137 20.0 91310117 696.7 99059673 755.8
> DT = data.table(col1 = 1:1e6)
> cols = paste0('col', 2:100)
> for (col in cols){ DT[, col := 1:1e6, with = F] }
There were 50 or more warnings (use warnings() to see the first 50)
>
> rm_and_release <- function(dt){
+ dt <- dt[sample(1e6, 9e5, FALSE)]
+ print(gc())
+ rm(list = "DT", envir = globalenv())
+
+ print(gc())
+ }
>
> gc()
used (Mb) gc trigger (Mb) max used (Mb)
Ncells 1076769 57.6 1899034 101.5 1899034 101.5
Vcells 53024698 404.6 91310117 696.7 99059673 755.8
> rm_and_release(DT)
used (Mb) gc trigger (Mb) max used (Mb)
Ncells 1075902 57.5 1899034 101.5 1899034 101.5
Vcells 97613454 744.8 134081733 1023.0 99059673 755.8
used (Mb) gc trigger (Mb) max used (Mb)
Ncells 1075901 57.5 1899034 101.5 1899034 101.5
Vcells 97613454 744.8 160978079 1228.2 99059673 755.8
> exists("DT")
[1] FALSE
> gc()
used (Mb) gc trigger (Mb) max used (Mb)
Ncells 1075669 57.5 1899034 101.5 1899034 101.5
Vcells 2613271 20.0 128782463 982.6 99059673 755.8
> ## [1] FALSEhttps://stackoverflow.com/questions/56361936
复制相似问题