object.size

object.size(x)
Estimate how much memory is being used to store a particular R object.
  • x – An R object.

Example. Below, vectors of normal random variables are created with lengths 10, 100, …, 10^7. As each object is created, its size is stored. These sizes are then plotted against the lengths of the vectors (saved using the png function). Finally, we examine how much extra memory is used to hold the vectors in a single list object (not much).
> x  <- list()
> xs <- rep(NA, 7)
> for(i in 1:7){
+ 	X      <- rnorm(10^i)
+ 	xs[i]  <- object.size(X)
+ 	x[[i]] <- X
+ }
> 
> 
> xs
[1]      152      824     8024    80024   800024  8000024
[7] 80000024
> png("objectsize.png")
> plot(10^(1:7), xs, log="xy",
+      xlab="Length", ylab="Memory")
> dev.off()
null device 
          1 
> 
> 
> object.size(x)
88889152 bytes
> object.size(x) - sum(xs)
56 bytes
As noted in the function description, the object size is an estimate. It may not be perfectly accurate.

Leave a Reply