R语言清除变量/清除所有变量/清理内存/清屏的方法,最后记得再用gc()

本文介绍了如何在R语言中清除单个及所有变量,使用rm()函数配合gc()函数进行内存清理,以及如何使用gc()函数获取内存使用报告。同时,讲解了清屏快捷键Ctrl + L的操作,它只清屏不删除变量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

清除单个变量

rm(object) #变量名

清除所有变量

rm(list = ls())

注意,使用rm() 函数后虽然会删除变量,但只是从R的工作环境中删除了这些变量,并没有在你的电脑储存空间中完全删除这些变量,所以这些变量还是会占用电脑内存空间的

你可以理解为rm() 类似把文件丢到了回收站,但没有从回收站永久删除他们

如果想完全删除,需要再用一次 gc() 函数,并且gc() 函数会report目前的内存使用情况

在这里插入图片描述
R 的官方文档里写着,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. For an accurate report full = TRUE should be used.

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.

R allocates space for vectors in multiples of 8 bytes: hence the report of “Vcells”, a relic of an earlier allocator (that used a vector heap).

所以可以用full = TRUE 这个参数来获得详细报告

清屏命令

Ctrl + L

只清屏, 并不会删除储存的变量名

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值