3.10 · Enum layout, existential boxing, tail allocation, slice retention, growth and the command-line tools

More of what memory physically looks like and how to look at it: how small enums and optionals pack, when a protocol value hits the heap, why a slice can pin a large array, and which tools inspect a process.

Mental model. Know three numbers for any type, size, stride and whether it allocates, and know one command for each way a process can hold memory.

Packing and boxing

Extra inhabitants, existential buffers and tail allocation.

  1. Why is Optional nine bytes but Optional eight, and how do enums choose their layout? 3.10.1 · L5
    A value’s bit pattern often has spare patterns that no valid value uses (extra inhabitants), and Swift stores enum cases and nil in them when it can. A class…
  2. How big is an any Protocol value, and when does storing one allocate on the heap? 3.10.2 · L5
    An any P for a non-class protocol is a 40-byte container on 64-bit: a three-word inline buffer for the value, a pointer to the type’s metadata, and a pointer…
  3. What is ManagedBuffer, and how does tail allocation put a header and elements in one allocation? 3.10.3 · L5
    ManagedBuffer<Header, Element> is a class whose instances are allocated with extra storage after the object (tail allocation). One malloc holds the 16-byte…

Growth and inspection

Slices that pin buffers, capacity rules and the macOS inspection tools.

  1. Why can a small slice keep a large array (or string) alive, and how do you stop it? 3.10.4 · L3
    An ArraySlice or Substring is a view onto the original buffer: it stores a range and a reference to the parent storage. The elements outside the range stay…
  2. How do Array capacity and growth work, and when does an array give memory back? 3.10.5 · L3
    An array has a count and a capacity. When an append exceeds capacity the array allocates a bigger buffer and moves the elements; the growth is geometric (the…
  3. Which command-line tools inspect a running process’s memory on macOS: leaks, heap, footprint and vmmap? 3.10.6 · L3
    Instruments is the friendly front end, but every view is also a command that you can script in CI or run on a Mac app or a simulator process (xcrun simctl…

More in Memory, ARC & runtime