【发布时间】:2012-11-02 13:00:28
【问题描述】:
我正在使用 mboost 包中的 blackboost 函数在 Windows 7 64 位、8gb RAM 机器上估计一个大约 500mb 数据集的模型。在执行期间,R 几乎使用了所有可用内存。计算完成后,即使在使用 gc() 调用垃圾收集或将工作区保存并重新加载到新的 R 会话后,仍有超过 4.5gb 分配给 R。使用 .ls.objects (1358003) 我发现所有可见对象的大小约为 550mb。
gc() 的输出告诉我大部分数据都在向量单元格中,尽管我不确定这意味着什么:
used (Mb) gc trigger (Mb) max used (Mb)
Ncells 2856967 152.6 4418719 236.0 3933533 210.1
Vcells 526859527 4019.7 610311178 4656.4 558577920 4261.7
这就是我正在做的:
> memory.size()
[1] 1443.99
> model <- blackboost(formula, data = mydata[mydata$var == 1,c(dv,ivs)],tree_control=ctree_control(maxdepth = 4))
...一堆包被加载...
> memory.size()
[1] 4431.85
> print(object.size(model),units="Mb")
25.7 Mb
> memory.profile()
NULL symbol pairlist closure environment promise language
1 15895 826659 20395 4234 13694 248423
special builtin char logical integer double complex
174 1572 1197774 34286 84631 42071 28
character ... any list expression bytecode externalptr
228592 1 0 79877 1 51276 2182
weakref raw S4
413 417 4385
mydata[mydata$var == 1,c(dv,ivs)] 有 139593 行和 75 列,其中大部分是因子变量和一些逻辑或数值变量。 formula 是以下类型的公式对象:“dv ~ var2 + var3 + .... + var73”。 dv 是一个变量名字符串,ivs 是一个包含所有自变量 var2 ... var74 的字符串向量。
为什么要为 R 分配这么多内存?如何让 R 释放额外的内存?任何想法表示赞赏!
【问题讨论】:
-
memory.size()报告什么? -
这可能是代码中的内存泄漏。您可以粘贴您正在调用的函数的示例(带有演示数据)吗?
-
“内存泄漏”到底是什么意思?我在我的问题中添加了一些示例代码。如果您需要更多信息,请告诉我。 @James:我在 blackboost 函数之前和之后添加了 memory.size() 输出到我的问题。
-
查看
?Memory了解Vcells 的解释:R maintains separate areas for fixed and variable sized objects. The first of these is allocated as an array of cons cells (Lisp programmers will know what they are, others may think of them as the building blocks of the language itself, parse trees, etc.), and the second are thrown on a heap of ‘Vcells’ of 8 bytes each.另外,memory.profile()返回什么? -
感谢@AriB.Friedman。我将 memory.profile() 输出添加到我的问题中,并将 gc() 输出更新到我当前的会话中。
标签: r memory-management