[R] object size vs. file size

Duncan Murdoch murdoch at stats.uwo.ca
Sat Mar 25 15:51:59 CET 2006

On 3/25/2006 7:32 AM, Steven Lacey wrote:
> Hi, 
> There is rather large discrepancy in the size of the object as it lives in R
> and the size of the object when it is written to the disk. The object in
> question is an S4 of a homemade class "sa". I first call a function that
> writes a list of these objects to a file called "data.RData". The size of
> this file is 14,411 KB. I would assume on average then, that each list
> component--there are 32 sa objects in data.RData--would be approximately 450
> KB (14,111/32). However, when I load the data into R and call object.size on
> just one s4 object (call it tmp) it returns 77496 bytes (77 KB)! What is
> even stranger is that if I save this S4 object alone by calling save(tmp,
> file="tmp.RData"), tmp.RData is 13.3 MB! I understand from the help on
> object.size that the object size is only approximate and excludes the space
> recquired to store its name in the symbol table. But, this difference in
> object size and file size is huge! This phenomenon occurs no matter which S4
> object I save from data.RData.
> Why is the object so big when it is in a file? What else is getting stored
> with it? I have examined the object in R to find additional information
> stored with it, but have not found anything that would account for the size
> of the object in the file system.
> For example, 
>> environment(tmp)
> I suspect somehow my code is attaching additional information to the object
> when it is saved. Does anyone know what is happening? Are there functions
> that will help me figure out what is going on? If I knew what was accounting
> for the size, then I would be better able to debug my code. Without knowing
> what else is getting saved, I don't know where to look.
> Thanks for any help you can provide. I am really lost on this one. 

I think you'll need to give an example if you want anything other than 
vague answers such as "it's storing more than you think it's storing".

Can't you post code to construct something with the same structure as 
the real object, and show us how you measured its size?

Duncan Murdoch

More information about the R-help mailing list