[R] Size of a refClass instance

David Kulp dkulp at fiksu.com
Wed May 1 20:20:02 CEST 2013

I'm using refClass for a complex multi-directional tree structure with possibly 100,000s of nodes.  The refClass design is very impressive and I'd love to use it, but I've found that the size of refClass instances are very large and creation time is slow.  For example, below is a RefClass and normal S4 class.  The RefClass requires about 4KB per instance vs 500B for the S4 class -- based on adding the Ncells and Vcells of used memory reported by gc().  And instantiation is more than twice as slow for a RefClass.  (R 2.14.2)

Anyone have thoughts on this and whether there's any hope for improving resources on either front?  

I wonder what others are doing.  I've been thinking about lightweight alternative implementations, but nothing particularly elegant has come to mind, yet!


simple <- 
              fields = list(a = "character", b="numeric")
system.time(simple.list <- lapply(1:100000, function(i) { simple$new(a='foo',b=i) }))

setClass('simple2', representation(a="character",b="numeric"))
setMethod("initialize", "simple2",
          function(.Object, a, b) {
            .Object at a <- a
            .Object at b <- b

system.time(simple2.list <- lapply(1:100000, function(i) { new('simple2',a='foo',b=i) }))

More information about the R-help mailing list