[R] Reference classes and memory consumption

Jan Mueller Jan.Mueller at ipsos.com
Thu Jun 21 12:01:03 CEST 2012


Dear Martin,

thank you for your hints. They were very helpful. Maybe memory.size() is a more reasonable measure of memory consumption?
Here's a rough comparison of 10.000 instances of a reference class, an S3 class and a simple vector:

Reference Class: 14MB
S3 class: 1,76MB
Vector: 0.08MB

So classes add some overhead but probably not as much as a simple saveRDS(..., compress=F) suggests.
Which means that my memory problem is probably not related to Reference Classes. I have not THAT many instances.

Best
Jan


The code I used to determine memory size:

# start new R session, empty workspace
untouched=memory.size();
MySmallClass = setRefClass("MySmallClass",
       fields = list(
 		myField = "numeric"
       ),
       methods = list(
 	   initialize = function(f) {
 	     myField<<- f;
           })	
 );
o = lapply(rnorm(10000), MySmallClass$new)
withobj=memory.size() 
print(paste("Initial Mem:", untouched, "With objects:", withobj, "Difference:", round(withobj-untouched, 2)));
# Output: [1] "Initial Mem: 14 With objects: 28 Difference: 14"
# 35.988K process size in Windows Task Manager


# start new R session, empty workspace
untouched=memory.size();
agent <- function(myField) {
    env = new.env(parent=emptyenv())
    env[["myField"]] = myField
    lockBinding("myField", env)  ## 'private' data?
    class(env) = "agent"
    env
}
agents = lapply(rnorm(10000), agent)
withobj=memory.size() 
print(paste("Initial Mem:", untouched, "With objects:", withobj, "Difference:", round(withobj-untouched, 2)));
# [1] "Initial Mem: 14.05 With objects: 15.81 Difference: 1.76"
# 22.336K process size in Windows Task Manager


# start new R session, empty workspace
untouched=memory.size();
arr=rnorm(10000);
withobj=memory.size() 
print(paste("Initial Mem:", untouched, "With objects:", withobj, "Difference:", round(withobj-untouched, 2)));
# [1] "Initial Mem: 14.05 With objects: 14.13 Difference: 0.08"
# 20.780K process size in Windows Task Manager



More information about the R-help mailing list