ECE344 Fall 2026 (Sec 1) Lec 29 - Advanced Memory Allocation
Overview
Jon Eyolfson compares general free-list allocation with faster strategies that restrict allocation sizes, including segregated size classes, per-thread caches, bump arenas, buddy allocation, and slab caches. He explains how Linux combines the buddy allocator for physical frames with SLUB caches for common kernel objects, then connects physical-memory compaction and virtual allocation to DMA, huge pages, and the limits of reference counting.
Key takeaways
- Segregated size classes trade internal fragmentation for fast allocation: each rounded size has its own free list, avoiding general-purpose fit searches and per-allocation coalescing.
- Per-thread caches and allocator arenas reduce contention by keeping many allocation and free operations local instead of locking one shared heap.
- Buddy allocation efficiently manages contiguous physical memory by splitting power-of-two blocks and merging only free buddies; XORing a block address with its size identifies its buddy.
- Linux layers SLUB object caches over buddy-allocated pages, using specialized slots for frequently allocated kernel structures and per-CPU caches to reduce locking.
- DMA requires physical addresses and may require contiguous frames, while vmalloc can provide contiguous virtual addresses backed by scattered physical pages.
- Reference counting frees an object when its count reaches zero but cannot reclaim unreachable cycles; weak_ptr can break ownership cycles, while tracing garbage collection detects unreachable groups.
Chapters
- The earlier general allocator supported arbitrary sizes with block headers and footers, fit searches, splitting, and neighbor coalescing.
- Searching for a fit costs time, and repeated allocation and freeing can fragment memory.
- Restricting supported sizes or freeing groups of allocations together can improve speed at the cost of flexibility.
- A segregated free-list allocator rounds requests to size classes such as 16, 32, and 48 bytes, with a separate free list for each class.
- Allocation and freeing can use constant-time list operations without searching, splitting, or coalescing within a class; rounding wastes space through internal fragmentation.
- A free block can store its next pointer in its otherwise unused payload, while a C union lets the same slot hold either that pointer or a user object.
- The lecture names jemalloc and mimalloc as examples of allocators designed to replace or improve standard C allocation.
- A single shared heap needs a lock when multiple threads modify its free lists, creating contention.
- The C library can use independent arenas, each with its own lock, and obtain arena memory with mmap rather than relying only on the single brk heap.
- A per-thread cache, or tcache, holds recently freed blocks by size class so its owning thread can reuse them without a shared lock.
- Freeing from a different thread may require returning a block to the arena it came from, adding synchronization.
- A bump allocator advances through a memory region and frees the entire region with a reset instead of freeing individual objects.
- This works well when allocations share a lifetime, such as temporary compiler data for one function, web-server data for one request, or frame data in a game.
- Resetting an arena is a single operation and avoids per-object headers, free lists, coalescing, and fragmentation within the arena.
- A kernel must allocate memory for structures such as page tables and kernel stacks, and some device buffers must occupy contiguous physical frames.
- DMA devices receive physical addresses rather than process virtual addresses, so a virtually contiguous region backed by scattered frames is not sufficient.
- The buddy allocator restricts block sizes to powers of two and finds a sufficiently large block, splitting it into halves until it reaches the requested order.
- The allocator keeps a free list for each power-of-two size; a 16-byte request can be served by splitting a larger block and returning unused halves to their size-class lists.
- When a block is freed, it merges with its buddy only if that specific adjacent partner is free, then checks whether the resulting larger block can merge again.
- For power-of-two blocks, a block's buddy address can be found by XORing its address with its size.
- The lecture demonstrates rounding a 28-byte request to 32 bytes, allocating a 32-byte request by splitting a 64-byte block, and coalescing freed blocks back into a larger block.
- Linux uses the buddy allocator for physical memory; each order in /proc/buddyinfo represents blocks twice the size of the previous order.
- The smallest order is one page, typically 4 KB, and larger orders include contiguous blocks such as 8 KB, 16 KB, and beyond.
- Linux separates memory into zones, including DMA zones for devices that cannot address all physical memory.
- Buddy allocation is fast and reduces external fragmentation within its blocks, but rounding to powers of two can create internal fragmentation.
- A slab cache keeps reusable slots sized for one object type, such as process control blocks, open-file structures, inodes, or buffers.
- A bitmap can track slot availability, or each free slot can hold a pointer to the next free slot.
- Linux builds object slabs from larger buddy-allocated regions; if an object size does not divide the slab size evenly, the leftover bytes are internal fragmentation.
- Linux uses SLUB, a simplified slab allocator with per-CPU caches, allowing common local allocations to avoid locks.
- Linux kmalloc serves small allocations, described in the lecture as roughly 8 bytes through 8 KB, using slab caches backed by the buddy allocator; larger allocations can come directly from buddy blocks.
- vmalloc creates a contiguous virtual address range from physical frames that may be scattered, so those allocations are unsuitable for devices requiring contiguous physical memory.
- Physical fragmentation can leave few large free blocks; Linux compaction moves used pages, updates page-table entries, and creates larger contiguous free regions.
- Transparent huge pages can map a 2 MB region with one TLB entry instead of 512 separate 4 KB entries, and programs can request memory advice with madvise.
- Reference counting stores the number of pointers to an object: creating a reference increments the count, dropping one decrements it, and reaching zero frees the object.
- The same idea appears in kernel reference counts, pipe-end behavior, C++ shared_ptr, and Rust atomic reference counting.
- A shared_ptr destructor releases its reference automatically, but an object's references must still be managed correctly.
- When a reference-counted node is freed, releasing its pointer to a successor can recursively free the rest of a non-cyclic structure.
- A cycle such as node C pointing to D and D pointing back to C keeps both reference counts above zero after the external reference is dropped, leaking the cycle.
- C++ weak_ptr provides a non-owning reference that does not increase the reference count; using it for back-pointers can break ownership cycles.
- Tracing garbage collectors identify unreachable objects by following pointers from program roots, so they can reclaim cycles that reference counting cannot.
- Garbage collection does not eliminate all memory retention: an object still reachable through an unnecessary reference may remain allocated.
Transcript excerpt
1:41 I >> All right, welcome back to operating
5:08 systems. So we get to carry on our journey. We are almost done. So this is aside from one more content lecture next week. It's pretty much reviewing our home free. And if you didn't see the announcement, no lecture Tuesday, so you can concentrate on the demos and have time for that. Hopefully I'll be able to show up for some of them, see what some of you guys cooked up. But let's continue on our journey for advanced memory management. So what we had before, we had thinking about what happened if we had to implement malloc ourselves. And we were like, okay, well, we could just have a free list and if we handle any size allocation, we have to keep track of the sizes on that free list. And
5:56 there was a header per block and a footer and it searched for a fit whenever we asked for an allocation. And we'd trim it if we needed to. We'd merge neighbors whenever we free. Any size works works, but it has some drawbacks where well, we have to search. Kind of lame, kind of slow, and it fragments over time. So today we get to restrict the problem, see how we can do things that will speed up our allocation. Kind of a taste of 454, although some of you are in 454. So some of the techniques are just supporting a few sizes, only supporting one size, or only supporting powers of two. Or a strategy is just to never free anything and only free stuff at one time.
6:44 So one technique is just giving size classes, like just have a whole bunch of size classes you support. So you could round up every request to one of a few size classes, like 16, 32,
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Jon Eyolfson.