Determine Memory Usage of Data Objects in R (2 Examples)


In this R tutorial you’ll learn how to report the space allocated to a data object using the object.size function.

Table of contents:

Let’s take a look at some R codes in action!


Creating Example Data

The first step is to define some data that we can use in the exemplifying code later on:

x1 <- 1                            # Create example data objects
x2 <- 1:10000
x3 <- 1:1000000000

After executing the previous R syntax, you should find three data objects called x1, x2, and x3 in the global environment of your R or RStudio session.


Example 1: Get Memory Used by One Data Object in Workspace

In Example 1, I’ll illustrate how to get the size of one specific data object in our workspace.

For this, we can apply the object.size function as shown below:

object.size(x1)                    # Apply object.size to single object
# 56 bytes

As you can see based on the previous output of the RStudio console, our example data object x1 has a memory size of 56 bytes.


Example 2: Get Memory Used by All Data Objects in Workspace

In Example 2, I’ll show how to identify data objects with large memory usage by creating a usage report for all objects in our workspace.

For this task, we have to use a combination of the sort, sapply, ls, function, object.size, and get commands.

sort(sapply(ls(), function(x) {    # Apply object.size to all objects
  object.size(get(x)) })) 
# x1         x2         x3 
# 56      40048 4000000048

Have a look at the previous output: It shows the memory size of all our data objects.


Video, Further Resources & Summary

If you need further explanations on the examples of this article, you may have a look at the following video of my YouTube channel. I explain the R syntax of this tutorial in the video:


The YouTube video will be added soon.


In addition, you might want to have a look at the other tutorials of my website. A selection of tutorials can be found below:


In this R tutorial you have learned how to apply the object.size function to determine the memory size of objects. Please let me know in the comments, if you have any additional questions.


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. Required fields are marked *

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