[Rd] Why does an empty vector occupy 40 bytes?

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Aug 29 17:10:44 CEST 2013


On 29/08/2013 14:39, Hadley Wickham wrote:
> Hi all,
>
> Why is the object size of an empty vector 40 bytes? (At least in 64-bit R.)
>
> object.size(integer(0))
> # 40 bytes
>
> Reading R internals, it looks like it should be:
>
> * 4 bytes: sxpinfo header (= 32 bits)
> * 8 bytes: pointer to attributes
> * 8 bytes: pointer to next node
> * 8 bytes: pointer to previous node
> * 4 bytes: length
> * 4 bytes: true length
>
> = 36 bytes
>
> Where are the extra 4 bytes coming from? What have I missed?
>
> I thought it might be for memory alignment, but if it was padded by an
> additional 4 bytes, then an integer vector of length 1 should be the
> same size, assuming I've interpreted the comment about "aligned as
> required" correctly.

You have not: the start of the vector area also needs to be aligned 
(since it might hold doubles or CHARSXPs).

See memory.c, which says, inter alia

/* All vector objects must be a multiple of sizeof(SEXPREC_ALIGN)
    bytes so that alignment is preserved for all objects */

/* Node Classes.  Non-vector nodes are of class zero. Small vector
    nodes are in classes 1, ..., NUM_SMALL_NODE_CLASSES, and large
    vector nodes are in class LARGE_NODE_CLASS.  For vector nodes the
    node header is followed in memory by the vector data, offset from
    the header by SEXPREC_ALIGN. */

And also, object.size() is only approximate, and documented to be so. In 
fact many short vectors are using larger blocks of memory incompletely, 
and of course the OS is supplying memory in pages.


-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-devel mailing list