R循环消耗内存

R循环消耗内存,r,memory-leaks,garbage-collection,officer,R,Memory Leaks,Garbage Collection,Officer,我正在使用officer库生成docx文件。 数据库不大(几十行和几十列)。 经过一定次数的迭代后,内存使用量急剧增加,最终R崩溃 我不能共享整个代码和数据库,所以我在这里添加了一种循环的框架代码。我希望这有助于理解这个问题: datasource(这是我第一次回答自己的问题。) 早些时候,我将officer调用包装在自定义函数中,这些函数都在读取和写入全局(.GlobalEnv)变量,包括包含大型officer文档的变量 结果表明,其中一个函数生成了全局变量的本地副本,该副本是: 当函数返回

我正在使用
officer
库生成docx文件。 数据库不大(几十行和几十列)。 经过一定次数的迭代后,内存使用量急剧增加,最终R崩溃

我不能共享整个代码和数据库,所以我在这里添加了一种循环的框架代码。我希望这有助于理解这个问题:

datasource(这是我第一次回答自己的问题。)

早些时候,我将
officer
调用包装在自定义函数中,这些函数都在读取和写入全局(
.GlobalEnv
)变量,包括包含大型
officer
文档的变量

结果表明,其中一个函数生成了全局变量的本地副本,该副本是:

  • 当函数返回时,R不会自动销毁
  • 重新运行函数时未在内存中覆盖
  • 未由
    gc()
    清理(
    gc()
    已运行,但没有帮助)
  • 直到
    rm()
    我才删除(我没有包括
    rm()
…它以一种指数的方式不断地乱扔记忆

在我的例子中,解决方案是避免制作函数本地的副本。或者,我认为,使用
rm()
显式删除对象也可以

现在,内存使用是平坦的,可以安全地运行任意数量的迭代


吸取的教训。不要指望别人来清理你的东西。

请阅读,你可以使用body\u add\u docxMaybe在gc()之前尝试用rm()删除所有将被删除的对象overwritten@SantiagoI.Hurtado这正是我正在做的…@DavidGohel,谢谢你。在我的例子中,这个问题是通过适当的变量范围来解决的。我以后可能还会使用body\u add\u docx。。。
Garbage collection 48 = 40+4+4 (level 2) ... 
58.1 Mbytes of cons cells used (55%)
15.0 Mbytes of vectors used (23%)
   > 2
Garbage collection 58 = 48+5+5 (level 2) ... 
58.1 Mbytes of cons cells used (55%)
15.0 Mbytes of vectors used (23%)
   > 3
Garbage collection 67 = 56+5+6 (level 2) ... 
58.1 Mbytes of cons cells used (55%)
15.0 Mbytes of vectors used (23%)
   > 4
Garbage collection 76 = 63+5+8 (level 2) ... 
58.1 Mbytes of cons cells used (55%)
15.0 Mbytes of vectors used (23%)
   > 5
Garbage collection 85 = 71+5+9 (level 2) ... 
58.2 Mbytes of cons cells used (55%)
15.0 Mbytes of vectors used (23%)
   > 6
Garbage collection 94 = 79+5+10 (level 2) ... 
58.2 Mbytes of cons cells used (55%)
15.0 Mbytes of vectors used (23%)
   > 7
Garbage collection 103 = 86+6+11 (level 2) ... 
58.2 Mbytes of cons cells used (56%)
15.1 Mbytes of vectors used (23%)
   > 8
Garbage collection 112 = 94+6+12 (level 2) ... 
58.2 Mbytes of cons cells used (56%)
15.1 Mbytes of vectors used (24%)
   > 9
Garbage collection 121 = 101+7+13 (level 2) ... 
58.2 Mbytes of cons cells used (56%)
15.2 Mbytes of vectors used (24%)
   > 10
Garbage collection 130 = 109+7+14 (level 2) ... 
58.2 Mbytes of cons cells used (56%)
15.4 Mbytes of vectors used (24%)
   > 11
Garbage collection 139 = 117+7+15 (level 2) ... 
58.3 Mbytes of cons cells used (56%)
15.8 Mbytes of vectors used (25%)
   > 12
Garbage collection 148 = 124+8+16 (level 2) ... 
58.4 Mbytes of cons cells used (56%)
16.6 Mbytes of vectors used (26%)
   > 13
Garbage collection 157 = 132+8+17 (level 2) ... 
58.5 Mbytes of cons cells used (56%)
18.4 Mbytes of vectors used (29%)
   > 14
Garbage collection 166 = 140+8+18 (level 2) ... 
58.7 Mbytes of cons cells used (56%)
22.2 Mbytes of vectors used (35%)
   > 15
Garbage collection 175 = 147+9+19 (level 2) ... 
59.2 Mbytes of cons cells used (56%)
30.4 Mbytes of vectors used (47%)
   > 16
Garbage collection 184 = 155+9+20 (level 2) ... 
60.0 Mbytes of cons cells used (57%)
47.6 Mbytes of vectors used (61%)
   > 17
Garbage collection 195 = 163+10+22 (level 2) ... 
61.8 Mbytes of cons cells used (59%)
83.7 Mbytes of vectors used (64%)
   > 18
Garbage collection 208 = 173+11+24 (level 2) ... 
65.3 Mbytes of cons cells used (62%)
160.5 Mbytes of vectors used (77%)
   > 19
Garbage collection 223 = 183+13+27 (level 2) ... 
72.3 Mbytes of cons cells used (56%)
321.7 Mbytes of vectors used (80%)
   > 20
Garbage collection 235 = 189+15+31 (level 2) ... 
86.3 Mbytes of cons cells used (55%)
659.3 Mbytes of vectors used (75%)
   > 21
Garbage collection 245 = 195+15+35 (level 2) ... 
114.4 Mbytes of cons cells used (58%)
1365.0 Mbytes of vectors used (73%)
   > 22
Garbage collection 255 = 199+17+39 (level 2) ... 
170.4 Mbytes of cons cells used (59%)
2837.6 Mbytes of vectors used (72%)
   > 23
Garbage collection 264 = 203+18+43 (level 2) ... 
282.4 Mbytes of cons cells used (58%)
5904.6 Mbytes of vectors used (72%)
   > 24
Killed