gc()和rm()之间的区别是什么 [英] What is the difference between gc() and rm()

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

问题描述

我通过调用 rm(list = ls())来定期清理R中的内存。

我需要调用垃圾回收器 gc()之后?

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

gc 不会删除任何 / em>你仍然使用的变量 - 它只会释放你无法访问的变量的内存(无论是使用 rm())还是被创建在一个已经返回的函数中)。运行 gc()永远不会让你失去变量。



是否应该调用<$ c $但调用 rm()之后,c> gc()是一个不错的选择。 gc的文档有助于说明:


调用gc会导致发生垃圾回收。这也将自动发生,无需用户干预,调用gc的主要目的是为了报告内存使用情况。



然而,在调用gc之后可能会很有用一个大对象已被删除,因为这可能会提示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?

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

解决方案

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.

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

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.

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.

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天全站免登陆