What’s the Difference Between the rm & gc Functions in R? (Example)

 

Sometimes, it might make sense to clean the memory in R (or RStudio) to get rid of unnecessary data that is slowing down your code.

Functions that are typically used for such tasks are the rm() and gc() functions.

However, when should those functions be used, and what is the difference between those functions?

This article explains how to apply cleaning functions such as rm and gc properly.

The content of this page looks as follows:

Let’s dive right into it!

 

When to Apply the rm() Function?

The rm function can be used to remove data objects from the R environment.

Let’s assume that we have created the three data objects x1, x2, and x3:

x1 <- 3             # Create some data objects
x2 <- 2
x3 <- 1

Then, our Global Environment looks as shown below:

 

r environment 1

 

Now, let’s assume that we want to clear our workspace to free up some memory. Then, we might use the rm and list functions as shown below:

rm(list = ls())     # Clear workspace

After running the previous R code, our global environment is empty:

 

r environment 2

 

So far so good – but why do we need the gc function? That’s what I’m going to show next!

 

When to Apply the gc() Function?

In contrast to the rm function, the gc function does not clear the workspace in RStudio.

However, a call of gc causes a garbage collection to take place, i.e. it cleans up the R memory without restarting the PC.

This can be especially useful after a large object has been deleted from the workspace, as this might prompt R to return memory to the operating system.

To achieve this, we simply can execute the gc function as shown below:

gc()                # Garbage collection

That’s it!

 

What’s the Difference Between rm() & gc()?

In the previous sections of this tutorial, I have explained how to apply the rm and gc functions in R.

As you might have noticed, the rm function is used to remove data objects from the environment.

In contrast, the gc function does not remove data objects from the environment. Instead, it cleans up the R memory and hence might be useful to speed up your code.

In other words: The gc function might be called AFTER using the rm function.

Note that in most cases a call of gc won’t be necessary. However, in case you are dealing with large data objects that use a lot of your computational power, a call of gc once in a while might be useful.

 

Video, Further Resources & Summary

Do you need additional information on the R programming code and the explanations of this article? Then I recommend checking out the following video that I have released on the Statistics Globe YouTube channel. I explain the contents of this tutorial in the video in some more detail.

 

The YouTube video will be added soon.

 

In addition, you may have a look at the other articles on Statistics Globe. You can find some related articles below:

 

This tutorial has shown how to apply the rm and gc functions in the R programming language. If you have any further questions, please let me know in the comments section below.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top