Tomiris
Tomiris

Reputation: 69

Conversion of pryr::mem_used() among memory units (MB, GB, ...)

pryr::mem_used() shows memory use as megabytes by default. Why does it convert the unit name (e.g. MB ->GB) after multiplication (or division) but not the value?

library(pryr)
mem_used()
  97.1 MB

mem_used()/1000
  97 kB 

mem_used()*1000
  97 GB

sessionInfo()
R version 3.2.0 (2015-04-16)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.9.5 (Mavericks)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] dplyr_0.4.2      data.table_1.9.4 pryr_0.1.2       readr_0.1.1      magrittr_1.5     XML_3.98-1.3    
[7] vegdata_0.7      foreign_0.8-63  

loaded via a namespace (and not attached):
 [1] Rcpp_0.11.6      codetools_0.2-11 assertthat_0.1   chron_2.3-47         plyr_1.8.3       R6_2.1.0        
 [7] DBI_0.3.1        stringi_0.5-5    reshape2_1.4.1   lazyeval_0.1.10      tools_3.2.0      stringr_1.0.0   
[13] parallel_3.2.0  

EDIT: This question refers to the way how the mem_used() output is formatted.

Upvotes: 3

Views: 746

Answers (2)

Ben Bolker
Ben Bolker

Reputation: 226732

Looking at class(mem_used()), we get "bytes". pryr:::print.bytes contains the following code:

power <- min(floor(log(abs(x), 1000)), 4)
if (power < 1) {
    unit <- "B"
}
else {
    unit <- c("kB", "MB", "GB", "TB")[[power]]
    x <- x/(1000^power)
}

So pryr computes the power/unit by taking the floor of the log (base 1000!) of the number of bytes. This is equivalent to seeing whether the number of bytes is larger than 1000, 10^6, 10^9, 10^12 ...

Upvotes: 2

OfficialBenWhite
OfficialBenWhite

Reputation: 141

Is this a question about conversion of memory units or how the output mem_used() is formatted?

Because 1024 kB -> 1 MB and 1024 MB -> 1 GB

And the output is class "bytes" which automatically scales the output to a nice number

Upvotes: 0

Related Questions