gc() 和 rm() 有什么区别 [英] What is the difference between gc() and rm()

查看:19
本文介绍了gc() 和 rm() 有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用对 rm(list=ls()) 的调用定期清理 R 中的内存.
之后我需要调用垃圾收集器 gc() 吗?

I am periodically cleaning the memory in R using a call to rm(list=ls()).
Do I need to call the garbage collector gc() after that?

这两个函数有什么区别?gc() 会为某些变量调用 rm() 吗?

What is the difference between these 2 functions? Does gc() call rm() for certain variables?

推荐答案

首先需要注意的是,两者的区别在于gc 不会删除任何 您仍在使用的变量 - 它只会为您不再访问的变量释放内存(无论是使用 rm() 删除还是在已返回的函数中创建).运行 gc() 永远不会让你丢失变量.

First, it is important to note that the two are very different in that gc does not delete any variables that you are still using- it only frees up the memory for ones that you no longer have access to (whether removed using rm() or, say, created in a function that has since returned). Running gc() will never make you lose variables.

在调用 rm() 之后是否应该调用 gc() 的问题是一个很好的问题.gc 的 文档 有用地指出:

The question of whether you should call gc() after calling rm(), though, is a good one. The documentation for gc helpfully notes:

调用 gc 会导致垃圾收集发生.这也会在没有用户干预的情况下自动发生,调用 gc 的主要目的是为了报告内存使用情况.

A call of gc causes a garbage collection to take place. This will also take place automatically without user intervention, and the primary purpose of calling gc is for the report on memory usage.

但是,在删除大对象后调用 gc 会很有用,因为这可能会提示 R 将内存返还给操作系统.

However, it can be useful to call gc after a large object has been removed, as this may prompt R to return memory to the operating system.

所以答案是调用 gc() 可能很好(并且至少不会伤害),即使它可能会被触发(如果不是马上,然后很快).

So the answer is that it can be good to call gc() (and at the very least, can't hurt), even though it would likely be triggered anyway (if not right away, then soon).

这篇关于gc() 和 rm() 有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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