R: Memory usage statistics by variable

Do you need a way to find out which individual variables in R consume the most memory?

# create dummy variables for demonstration
x <- 1:1000
y <- 1:10000
z <- 1:100000

# print aggregate memory usage statistics
print(paste('R is using', memory.size(), 'MB out of limit', memory.limit(), 'MB'))

# create function to return matrix of memory consumption
object.sizes <- function()
	return(rev(sort(sapply(ls(envir=.GlobalEnv), function (object.name) 

# print to console in table format

# draw bar plot
	main="Memory usage by object", ylab="Bytes", xlab="Variable name", 

# draw dot chart
dotchart(object.sizes(), main="Memory usage by object", xlab="Bytes")

# draw pie chart
pie(object.sizes(), main="Memory usage by object")

While trying to fit models to a large data set (using ctree and cforest in the party package), I kept running out of memory. I was surprised to learn the model—which contains just rules, right?—consumes much more memory than the original input data frame.

Using the memory.limit() function I increased R’s memory limit from 1500MB to 2000MB, but too bad 32-bit Windows inherently has a small memory limit.

I found the following links helpful related to R memory usage:


3 thoughts on “R: Memory usage statistics by variable

  1. Pingback: Od Informacji do Wiedzy » Archiwum bloga » R stronki

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s