rm() remove objects

What are some reasons for removing objects using rm() - is it absolutely necessary? or is it just to "clean" up the work space.

I generally use the rm() function to clean temporary variables that are no longer needed (just to clean up as you said). It also allows you to selectively remove the variables in the specific environments.

I only use it in interactive sessions to make sure nothing depends on poorly named temporary variables (like xx or xxx). The only good reason to use it in a program is if

  1. R runs out of RAM for creating new objects, and
  2. There are large objects that can be safely deleted.

Because "large" and "important" tend to be correlated, this doesn't happen much.

The nitty gritty of memory management: rm(x) just removes x as a reference to whatever value's behind it. Space will only be freed if no more references to a value exist whenever R does a clean-up sweep; you can force a sweep with gc().

R's memory management rules should be considered unreliable implementation details. They'll try to do what's efficient without getting in the way of the R language. And they may change in the future as people keep trying to squeeze more efficiency out the implementation. Calling gc() is more like asking a favor than issuing a command.

If you're interested in learning more or seeing examples, check out Advanced R's "Names and Values" section.

5 Likes

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.