How memory works in R

Hi all,

Need your help in understanding memory usage in R.
As per this link Memory usage · Advanced R. "

It says " object_size() tells you the size of a single object, pryr::mem_used() tells you the total size of all objects in memory:"

But I did an experiment to make sure this works as mentioned. But I see some differences.

> new_asd <- iris %>% group_by(Species) %>% summarise(Sepal.Length = sum(Sepal.Length))
> new_asd_1 <- iris %>% group_by(Species) %>% summarise(Sepal.Length = sum(Sepal.Length))
> mem_used()
134 MB
> object_size(new_asd, new_asd_1)
2,272 B

Please see above, the total memory and objects size of all the objects are not same. I also created another object

> new_asd_2 <- iris %>% group_by(Species) %>% summarise(Sepal.Length = sum(Sepal.Length))
> object_size(new_asd, new_asd_1, new_asd_2)
2,960 B
> mem_used()
134 MB

Still the mem_used is same 134MB. Not sure why???

The short answer is that R allocates space for vectors in multiples of 8 bytes, so not stepwise, not continuous increases.

# new session
pryr::mem_used()
#> 49.1 MB
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(pryr)
# after loading libraries
mem_used()
#> 55.2 MB
new_asd <- iris %>% group_by(Species) %>% summarise(Sepal.Length = sum(Sepal.Length))
# small object
object_size(new_asd)
#> 1,584 B
# but not quite a rounding error
mem_used()
#> 57.5 MB
new_asd_1 <- iris %>% group_by(Species) %>% summarise(Sepal.Length = sum(Sepal.Length))
# another small object
object_size(new_asd_1)
#> 1,584 B
# this time doesn't move the needle
mem_used()
#> 57.5 MB
# sum is less than its parts, probably because iris is duplicated
object_size(new_asd, new_asd_1)
#> 2,272 B
# nothing has been added, so needle doesn't move
mem_used()
#> 57.5 MB

Thank. But I did not understand your last step

# sum is less than its parts, probably because iris is duplicated
object_size(new_asd, new_asd_1)
#> 2,272 B
# nothing has been added, so needle doesn't move
mem_used()
#> 57.5 MB

57.5 MB - 0.002272MB = 57.49773 MB
Where is this remaining memory getting consumed?

Ask yourself, what is this number to 1 decimal place ?

Edit:
It seems some confusion by virtue that it is not only user created R objects that use session memory, but also R packages loaded that do.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.