Lesson 6 of 9 · 22m
Alignment, Allocation, and Paging
The quiet costs underneath malloc: alignment, allocator behavior, page faults, TLB pressure, and when huge pages or arenas are worth it.
Allocation feels like a language detail until you profile a server and find it spending its life inside malloc, page faults, or memory fragmentation. The hardware and operating system impose structure: objects have alignment requirements, memory arrives in pages, virtual addresses need translation, and allocators add their own policies. Performance work means cooperating with all three layers.
Alignment is a contract with the load/store unit
A type usually must be aligned to its size or a platform-defined boundary:
struct header {
char tag; // offset 0
// 3 bytes padding
int length; // offset 4
char flags; // offset 8
// padding to alignment may follow
};Compilers insert padding so loads are naturally aligned. Reordering fields from largest to smallest often shrinks structs, but do not optimize layout blindly — hot fields should remain near each other even if that costs a few bytes. SIMD loads may require 16-, 32-, or 64-byte alignment depending on instruction and platform.
malloc is not free even when memory is plentiful
A general-purpose allocator must handle varying sizes, thread safety, reuse, fragmentation, and system calls. Fast paths are thread-local caches in modern allocators, but allocation-heavy hot loops still cost instructions and metadata. Worse, allocation order determines memory locality. Objects created together often live near each other; objects created across phases can interleave badly.
An arena or pool is the practical answer when lifetimes line up:
Arena scratch;
arena_init(&scratch, 1 << 20);
for each request {
void *mem = arena_alloc(&scratch, bytes);
// use objects only for this request
}
arena_reset(&scratch);Arenas trade individual object deletion for bulk lifetime management. That is a feature when you can state lifetimes honestly.
Pages and the TLB
Virtual memory is managed in pages, commonly 4 KiB. Every memory access conceptually needs virtual-to-physical translation; the translation lookaside buffer (TLB) caches recent translations. Huge working sets spread over many pages can miss the TLB even when data is cache resident. Large pages — often 2 MiB on Linux x86 — reduce translation entries for big contiguous regions.
Linux huge pages can be configured explicitly or through Transparent Huge Pages:
cat /sys/kernel/mm/transparent_hugepage/enabled
echo madvise | sudo tee /sys/kernel/mm/transparent_hugepage/enabledDatabases and in-memory stores sometimes benefit, but THP can also cause latency spikes or fragmentation depending on workload. Measure; do not cargo-cult the setting.
First touch is real allocation
On many systems, allocating memory does not immediately populate physical pages. The first write triggers a page fault. In multithreaded programs, first-touch placement can also determine NUMA locality. Pre-faulting or initializing memory in the thread that will use it can be a meaningful optimization.
Field notes
sizeofreveals padding;alignof/offsetofreveal layout assumptions. Check them before guessing.- Allocating many tiny objects separately is both allocator overhead and locality loss.
- Memory-mapped files and lazy paging make “memory use” nuanced; huge pages help large dense regions, not pointer chasing.
- Custom allocation is a liability. Reach for arenas/pools when lifetimes are clear, not because custom memory management feels macho.
Was this lesson helpful?