[R] Reference classes and memory consumption

Martin Morgan mtmorgan at fhcrc.org
Thu Jun 21 05:20:36 CEST 2012


On 06/19/2012 02:22 AM, Jan Mueller wrote:
> Dear All,
>
> It seems that reference classes consume a lot of memory which became a problem for my rather extensive simulation.
> I prepared a small example. An instance of this minimal class uses about 20K on disk.
>
> rm(list=ls(all=TRUE));
>
> MySmallClass = setRefClass("MySmallClass",
>       fields = list(
> 		myField = "numeric"
>       ),
>       methods = list(
> 	   initialize = function(f) {
> 	     myField<<- f;
>           })	
> );
>
> o = MySmallClass$new(10);
> saveRDS(o, file="C:\\o.rds");
> file.info("C:\\o.rds")$size;
>
>
> Result:
> [1] 21274
>
> My questions:
> Is saveRDS() is representative for memory usage?
> If so, is there a way to shrink these objects to a smaller size?

Caution: my answer is little more than speculation.

usually one would use

   object.size(o)

to see the (approximate) size of an object, but reference classes use 
environments, and object.size does not help here. Poking around at the 
structure of reference classes, the environment is in a slot .xData, and 
the content of the slot can have their sizes determined, as

 > unlist(eapply(o at .xData, object.size, all.names=TRUE))
        .self         show     getClass .refClassDef
          656         5576         2456        48424
   .->myField   initialize      myField        field
           48         1952         5592         5144

and so we discover that the reference class instance carries with it 
it's own definition .refClassDef (!). So I think the answer is no, there 
is no way to shrink these instances. One could create a light-weight S3 
class to represent agents, say, in some kind of simulation as

   agent <- function(myField) {
       env = new.env(parent=emptyenv())
       env[["myField"]] = myField
       lockBinding("myField", env)  ## 'private' data?
       class(env) = "agent"
       env
   }
   agents = lapply(rnorm(1000), agent)

or re-organize your simulation to act on vectors rather than individuals

   .agents = setRefClass("Agents", fields = list(myField="numeric"))
   agents = .agents$new(myField=rnorm(n))

and win by (a) amortizing the size of the reference class over many 
agents and (b) setting yourself up for efficient vector calculations.

Martin

> I use R 2.15.0 on a 32 Bit Windows 7 system.
>
> Best regards,
> Jan
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.


-- 
Computational Biology / Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N.
PO Box 19024 Seattle, WA 98109

Location: Arnold Building M1 B861
Phone: (206) 667-2793



More information about the R-help mailing list