[Rd] Reference classes use a lot more memory after serialization/unserialization

Jan Mueller Jan.Mueller at ipsos.com
Tue Sep 4 09:56:11 CEST 2012


Hello All,

I posted this earlier on R-help but received no answers. This touches the implementation of reference classes so maybe R-devel is a better place.
The memory consumption of reference classes seems to increase dramatically (roughly 10x in the example below) with serialization/unserialization.
S4 - classes are apparently not affected. 
Any ideas how this could be solved or at least circumvented?

Best
Jan

# start new R session, empty workspace

gc();
MySmallClass = setRefClass("MySmallClass",
       fields = list(
 myField = "numeric"
       ),
       methods = list(
    initialize = function(f) {
      myField<<- f;
           })
 );

# Generate 10K instances
o = lapply(rnorm(10000), MySmallClass$new)

gc()
s=serialize(o, connection=NULL)
gc();
p=unserialize(s);
gc();


Output:
1st gc():
         used (Mb) gc trigger (Mb) max used (Mb)
Ncells 170895  4.6     350000  9.4   350000  9.4
Vcells 162042  1.3     905753  7.0   786155  6.0

2nd gc():
         used (Mb) gc trigger (Mb) max used (Mb)
Ncells 720320 19.3     984024 26.3   899071 24.1
Vcells 588993  4.5    1162592  8.9   786155  6.0

3rd gc():
          used (Mb) gc trigger (Mb) max used (Mb)
Ncells  720472 19.3    1166886 31.2   899071 24.1
Vcells 9941697 75.9   11298137 86.2  9941716 75.9

4th gc():
           used  (Mb) gc trigger  (Mb) max used  (Mb)
Ncells  6980212 186.4    7700734 205.7  6981388 186.5
Vcells 11744776  89.7   17456418 133.2 11754663  89.7

# 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-devel mailing list