[R] Reference classes and memory consumption

Jan Mueller Jan.Mueller at ipsos.com
Mon Aug 27 18:05:05 CEST 2012


I found something that looks a bit odd to me:

The memory consumption of reference classes seems to increase dramatically (roughly 10x in the example below) with serialization/deserialization (saveRDS() / readRDS()).
Maybe creating instances of the same class has a synergy effect (references?) which is lost after a saveRDS()/readRDS() cycle? Class definitions spring to mind but I was unable to track 
down any size difference along these lines.
Any ideas?

Best
Jan

# start new R session, empty workspace
untouched=memory.size();
MySmallClass = setRefClass("MySmallClass",
       fields = list(
 myField = "numeric"
       ),
       methods = list(
    initialize = function(f) {
      myField<<- f;
           })
 );

# Generate 10K instances
o = lapply(rnorm(10000), MySmallClass$new)
withobj=memory.size()
print(paste("Initial Mem:", untouched, "With objects:", withobj, "Difference:", round(withobj-untouched, 2))); 
#[1] "Initial Mem: 14.06 With objects: 29.14 Difference: 15.08"

 
# Save and read back those instances
saveRDS(o, "test.rds")
untouched=memory.size();
p=readRDS("test.rds")
withobj=memory.size()
print(paste("Initial Mem:", untouched, "With objects:", withobj, "Difference:", round(withobj-untouched, 2))); 
#[1] "Initial Mem: 31.2 With objects: 200.71 Difference: 169.51"


# Platform info:
# R version 2.15.1
# Platform: i386-pc-mingw32/i386 (32-bit)
# Binary package from CRAN
# Windows 7 32 Bit 
#  (I observed a similar behavior on Linux 64 Bit, same R version)



More information about the R-help mailing list