object.size vs lobstr::obj_size in R

x <- runif(1e6)
y <- list(x, x, x)

object.size(y) 
24000224 bytes

lobstr::obj_size(y)
8,000,128 B

Please explain what makes these two functions different.

Upvotes: 6

Views: 393

Answers (1)

Anindya Mozumdar
Anindya Mozumdar

Reputation: 523

This is because object.size does not account for shared references. The object y does not comprise 3 different copies of x but 3 references to the same copy of x.

This can also be verified by the function ref(y) from the lobstr package where you can see that the 3 different components of y have the same address in memory.

Thus object_size is reporting approximately the same size for x and y, whereas object.size is multiplying the size by 3.

Upvotes: 6

Related Questions