5.3 Allocation and Object Layout

MEMORYSTATUS: DEVELOPINGLEVEL: INTERMEDIATEREVIEWED: 2026-11-18

An allocator decides where each block of memory goes in the heap. The compiler, or the runtime that defines the object, decides how the fields inside the block are laid out. Both choices cost memory: a poor layout spends bytes on padding and headers, and a scattered heap causes cache misses and fragmentation. SEXP structures show both costs.

5.3.1 Allocation as a three-step process

When code calls malloc(n):

  1. The allocator finds a free block of at least n bytes.
  2. It marks that block as used.
  3. It returns a pointer to the block.

Later, free(p) marks the block as free, and many allocators merge it with free neighbors.

Different allocators implement steps 1 and 2 differently. The simplest is a free list: a linked list of free blocks. Allocation searches the list for the first block large enough. Deallocation inserts the block back into the list.

More sophisticated allocators use buddy trees, size classes, or per-thread arenas. All solve the same problem: track free memory and find a suitable piece quickly.

a free list threads through the heap next free block in use free in use free in use free list head malloc(n) walks the list, takes the first free block of at least n bytes, and marks it in use free(p) marks the block free, links it back into the list, and merges it with free neighbors

Free blocks form a linked list threaded through the heap. malloc takes a block off the list and free puts it back.

5.3.2 Allocation overhead

Every allocated block has overhead. The allocator needs metadata: the block size, whether it is free, and links to the next block in the free list.

glibc’s malloc on 64-bit Linux is a concrete case. It keeps an 8-byte size word in front of every block and rounds each block up to a multiple of 16 bytes, so a 64-byte request occupies 80 bytes. Its smallest block is 32 bytes, which is what malloc(0) gets.

For small allocations, overhead is significant: an 8-byte request still takes a 32-byte block.

For large allocations, overhead is negligible.

Size-class allocators such as jemalloc and mimalloc cut both costs. They hand out only certain sizes, keep blocks of one size together on the same pages, and record the size once per page instead of in a header on every block. With power-of-two classes (8, 16, 32, 64, 128 bytes, and so on), a 100-byte request gets 128 bytes. Wasting 28 bytes is cheaper than searching a free list.

GNU R manages memory for small objects itself instead of calling malloc for each one. In memory.c, non-vector nodes such as cons cells and environments are node class 0, a fixed 56-byte SEXPREC on a 64-bit build. Small vectors fall into classes 1 to 5, which hold up to 8, 16, 32, 64 and 128 bytes of data, and each class gets its own pages of about 8,000 bytes, taken from malloc. A vector with more than 128 bytes of data is class 7 and gets a malloc block of its own. Class 6 is for vectors from custom allocators.

5.3.3 Alignment and padding

Every type has an alignment, and a value’s address must be a multiple of it. On x86-64, a 64-bit integer or a double is aligned to 8 bytes and a 32-bit integer to 4.

The hardware is the reason. x86-64 handles a misaligned load, sometimes more slowly, while some ARM and older RISC processors fault on one. In C, a misaligned access is undefined behavior on any processor.

Alignment is kept in two places. malloc returns addresses aligned for any built-in type, which is 16 bytes on x86-64 Linux and Windows. Inside a struct, the compiler inserts padding so that each field lands on its own alignment:

#include <stddef.h>

struct loose { char tag; double value; char flag; };
struct tight { double value; char tag; char flag; };

/* gcc checks these at compile time on x86-64 */
_Static_assert(offsetof(struct loose, value) == 8, "7 padding bytes after tag");
_Static_assert(sizeof(struct loose) == 24, "7 more after flag");
_Static_assert(sizeof(struct tight) == 16, "6 at the end");

struct loose has 7 bytes of padding after tag, so that value starts on an 8-byte boundary, and 7 more after flag, so that the next element of an array of these structs is aligned too. Putting the largest field first cuts the size from 24 bytes to 16. Padding wastes memory but avoids slow or undefined memory access.

5.3.4 Object layout and cache lines

Objects are laid out to minimize cache misses. A cache line is typically 64 bytes. If an object fits on one cache line, reading it requires one memory access. If it spans two cache lines, it requires two accesses.

Size-class allocators place blocks of one size next to each other, so small objects allocated one after another often share a cache line.

For larger objects (vectors, data frames), layout determines access patterns. A contiguous vector is accessed sequentially. Dereferencing pointers within the vector causes cache misses if the pointed-to objects are scattered.

5.3.5 SEXPREC layout

A SEXP points to a SEXPREC structure. SEXPREC is the core data structure of R. Its layout determines the overhead of every R object.

/* R's internal layout, from src/include/Defn.h in R 4.6.1. This is NOT
   what packages see: the public Rinternals.h declares only
   `typedef struct SEXPREC *SEXP;` and leaves the struct undefined. */
struct sxpinfo_struct {
  unsigned int type   :  5;
  unsigned int scalar :  1;
  unsigned int obj    :  1;
  unsigned int alt    :  1;
  unsigned int gp     : 16;
  unsigned int mark   :  1;
  unsigned int debug  :  1;
  unsigned int trace  :  1;
  unsigned int spare  :  1;
  unsigned int gcgen  :  1;
  unsigned int gccls  :  3;
  unsigned int named  : 16;
  unsigned int extra  : 16;
};

struct primsxp_struct { int offset; };
struct symsxp_struct  { struct SEXPREC *pname, *value, *internal; };
struct listsxp_struct { struct SEXPREC *carval, *cdrval, *tagval; };
struct envsxp_struct  { struct SEXPREC *frame, *enclos, *hashtab; };
struct closxp_struct  { struct SEXPREC *formals, *body, *env; };
struct promsxp_struct { struct SEXPREC *value, *expr, *env; };

typedef struct SEXPREC {
  struct sxpinfo_struct sxpinfo;
  struct SEXPREC *attrib;
  struct SEXPREC *gengc_next_node;
  struct SEXPREC *gengc_prev_node;
  union {
    struct primsxp_struct primsxp;
    struct symsxp_struct  symsxp;
    struct listsxp_struct listsxp;
    struct envsxp_struct  envsxp;
    struct closxp_struct  closxp;
    struct promsxp_struct promsxp;
  } u;
} SEXPREC;

Vectors do not use that union. They carry a length and a true length after the header, followed by their payload, which is why the two shapes are usually drawn separately.

Count the header. sxpinfo is one 64-bit word. It widened to 64 bits in R 3.5.0, when the header layout changed to merge ALTREP, and since R 4.0.0 its 16-bit named field holds the reference count. attrib is a pointer, 8 bytes on a 64-bit build. The two generational GC list pointers are 8 bytes each.

That makes 32 bytes of header on a 64-bit build. A non-vector node adds its three-pointer union, for 56 bytes in all. A vector adds its length and true length instead, so 48 bytes come before its first element. object.size() shows the vector case:

object.size(integer(0))      # 48 bytes: the vector header alone
object.size(c(1L, 2L, 3L))   # 64 bytes: 12 bytes of data, stored in a 16-byte class
object.size(integer(1000))   # 4048 bytes: too big for the small classes

Three integers carry 48 bytes of header for 12 bytes of data, four times the data. For 1,000 integers, which R allocates with malloc, the same 48 bytes are about 1.2 percent of the total.

Every SEXPREC starts with this header, which includes GC mark bits, attributes, type, and flags. The overhead applies to every object, whatever its size.

5.3.6 Layout and multi-interpreter heaps

Per-interpreter heaps do not let each interpreter choose its own layout. Every interpreter in a process runs the same compiled R binary, and that code reads SEXPREC fields at fixed offsets, so one layout holds for the whole process. A separate heap changes where objects go and when they are collected, not their shape.

5.3.7 Fragmentation

Fragmentation occurs when free blocks scatter across the heap. If the heap has 100 MB free but no contiguous 50 MB block, a 50 MB request cannot use any of it. On a 64-bit system the allocator then maps fresh pages from the operating system, so the cost shows up as a larger memory footprint, not a failed allocation. A C allocator cannot move live blocks to close the gaps.

External fragmentation: free blocks are split. Allocating 100 bytes of a 200-byte block leaves 100 bytes as a separate free block. Over time, many small fragments accumulate.

Internal fragmentation: allocated blocks are larger than requested. Allocating 100 bytes in an allocator with power-of-two classes gives 128 bytes. The 28 bytes are wasted.

R’s node classes keep same-size small vectors together, which limits external fragmentation. Internal fragmentation remains, since a vector rounded up to its class size wastes the difference.

GNU R does not group objects by lifetime. Its small-vector pages are grouped by size class, and the collector hands a page back to malloc only when no node on it is in use, so a few live vectors can keep a mostly empty page in memory.

5.3.8 Per-interpreter heaps and layout

threaded-r gives each interpreter its own heap, with its own free lists and pages. Interpreters allocate without contending for one allocator, and each collector works on its own objects.

The cost is memory: every heap keeps its own partly used pages, so two interpreters hold more slack than one.

Further reading

Where this appears

  • threaded-r SEXPREC layout determines per-object overhead, and every interpreter in the process shares that one layout.
  • r-in-rust (rrs) Object layout is explicit. The Rust struct encoding directly maps to memory layout.

Where to go next

Back to top