使用 gc() 命令强制垃圾收集在 R 中运行 [英] Forcing garbage collection to run in R with the gc() command

查看:23
本文介绍了使用 gc() 命令强制垃圾收集在 R 中运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常草率地编程.好吧,我一直在草率地编程,但有时会以内存不足错误的形式赶上我.我开始练习使用 rm() 命令删除对象,事情变得更好了.我在网上看到关于在删除大型数据对象后是否应该显式调用 gc() 的混合消息.有人说在 R 返回内存错误之前它会运行 gc() 而其他人说手动强制 gc 是个好主意.

Periodically I program sloppily. Ok, I program sloppily all the time, but sometimes that catches up with me in the form of out of memory errors. I start exercising a little discipline in deleting objects with the rm() command and things get better. I see mixed messages online about whether I should explicitly call gc() after deleting large data objects. Some say that before R returns a memory error it will run gc() while others say that manually forcing gc is a good idea.

我应该在删除大对象后运行 gc() 以确保最大的内存可用性吗?

Should I run gc() after deleting large objects in order to ensure maximum memory availability?

推荐答案

可能."我也这样做,甚至经常像在循环中那样

"Probably." I do it too, and often even in a loop as in

cleanMem <- function(n=10) { for (i in 1:n) gc() }

然而,根据我的经验,这并不能将记忆恢复到原始状态.

Yet that does not, in my experience, restore memory to a pristine state.

所以我通常做的是将手头的任务保存在脚本文件中,并使用r"前端(在 Unix 上,并来自littler"包)执行那些任务.Rscript 是其他操作系统的替代品.

So what I usually do is to keep the tasks at hand in script files and execute those using the 'r' frontend (on Unix, and from the 'littler' package). Rscript is an alternative on that other OS.

该工作流程恰好同意

我们之前在这里介绍过.

which we covered here before.

这篇关于使用 gc() 命令强制垃圾收集在 R 中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆