ECE344 Fall 2026 (Sec 1) Lec 28 - Basic Memory Allocation
Overview
Jon Eyolfson explains how C library allocators subdivide the kernel’s 4 KiB pages into dynamically allocated blocks, contrasting static and stack allocation with heap-based malloc and free. He develops a bump allocator, then a free-list design using headers, footers, 16-byte alignment, fit strategies, splitting, and coalescing, before connecting glibc allocations to brk and mmap and reviewing common memory errors.
Key takeaways
- A bump allocator is a minimal working design: round requests to 16-byte boundaries, advance one heap offset, and accept that free cannot reclaim individual allocations.
- External fragmentation can prevent a 96-byte allocation even when 96 bytes are free in total, because those bytes may be split across noncontiguous blocks.
- Headers and footers let a free-list allocator track block sizes and find neighboring blocks; boundary tags enable constant-time coalescing without searching the entire list.
- First fit is an effective simple policy because it stops at the first suitable free block, while best fit may create unusably small holes and worst fit often performs poorly in realistic simulations.
- Returning a pointer to a local variable is undefined behavior even when the old stack bytes remain readable; heap allocation is required when returned data must outlive its function.
- glibc often retains freed small allocations for reuse within the process heap, whereas large mmap-backed allocations can be returned to the kernel with munmap.
Chapters
- The kernel’s basic allocator hands out 4 KiB pages from a free list, while ordinary C programs request smaller amounts such as 100 bytes.
- The C library’s malloc and free divide those pages into smaller blocks and manage their reuse.
- When physical frames run out, the operating system may evict pages to disk or use an out-of-memory killer.
- A global array has a fixed address determined by the compiler and exists for the lifetime of the program.
- Initialized global variables generally reside in the data section, while uninitialized globals are placed in BSS and zero-initialized.
- Static allocation wastes space when memory is only conditionally needed and cannot accommodate input-dependent sizes without reserving a maximum.
- A local variable such as int x uses space in the function’s stack frame, which the compiler manages automatically.
- alloca can request a variable number of bytes from the stack, but those bytes are reclaimed when the function returns.
- Returning a pointer to a local variable invokes undefined behavior; one compiler build warned about the code and produced a null pointer instead.
- A returned stack address may appear to work because returning from a function usually resets the stack pointer without clearing the old bytes.
- Later calls can reuse or overwrite that stack region, so a dangling pointer’s apparent value can change unpredictably.
- To return memory that outlives a function, allocate it on the heap with malloc and eventually release it with free.
- A bump allocator tracks one offset into a heap region, rounds requests up to 16-byte boundaries, and advances the offset for each allocation.
- If a request exceeds the remaining capacity, the allocator returns null; its free operation does nothing.
- Resetting the offset reclaims the entire region at once, but otherwise allocations are never reused and the allocator eventually runs out of space.
- Heap allocation is especially prone to fragmentation when allocations have different lifetimes, different sizes, and cannot be moved after pointers are returned.
- External fragmentation is free space in separate holes that cannot satisfy one contiguous request; internal fragmentation is unused space within an allocated block.
- A compacting garbage collector can move objects and update references, but a C allocator generally cannot relocate blocks while preserving arbitrary pointers.
- A free list links available blocks and records each block’s size so an allocator can find space for a request.
- A representative block layout uses a 16-byte header for size and allocation status, aligned user data, and an 8-byte footer that repeats the size.
- The footer, also called a boundary tag, lets the allocator locate the previous block; the header and block size locate the next one.
- malloc provides alignment suitable for types such as max_align_t, so requests and total block sizes are rounded to 16-byte boundaries in the example.
- Best fit selects the smallest free block that can satisfy a request, usually requiring a full-list search unless it finds an exact match.
- Worst fit selects the largest available block, while first fit stops at the first block large enough for the request.
- In the example with free blocks of 160 and 96 bytes, best fit places a 64-byte block in the 96-byte space, leaving holes that cannot fit a later 96-byte request.
- First fit is fast and tends to perform reasonably in practice, while simulated worst fit often produces the poorest storage utilization.
- When a free block is larger than a request, the allocator can split it into an allocated block and a smaller free block.
- A remainder too small to hold a useful block is not split off, creating internal fragmentation instead.
- Coalescing merges adjacent free blocks when memory is released, using the next block’s header and the previous block’s footer to find neighbors in constant time.
- glibc commonly obtains memory for small allocations by extending the process heap with the brk system call.
- Large allocations can instead use mmap, and freeing such a mapped allocation can lead to an munmap system call.
- Freeing a small heap allocation usually marks its block available for reuse without returning the heap space to the kernel.
- The strace example distinguishes these brk, mmap, and munmap calls, which operate on virtual addresses.
- Demand paging and overcommit allow a process to request more memory than it has physically touched; touching too much can still exhaust memory and trigger the out-of-memory killer.
- Common C memory errors include use-after-free, double free, and leaks caused by never calling free.
- The standard C library may detect a double free and abort, while tools such as Valgrind and AddressSanitizer can catch additional errors with runtime or compiler-inserted checks.
Transcript excerpt
6:49 Alrighty, welcome back to operating systems. We got basic memory allocation today. Fun. So, let's get into it. So, recall for the kernel, the kernel is really dumb right now. It just is handing out whole pages at a time. We saw in lecture 11 that BRK system call that is growing the heap in our user programs. How the colonel implemented it pages implement or m map that asks for pages from the kernel. What does the kernel do? Gives out pages. Giving out pages is easy. It's just a free list of pages. One page is as good as any other. They're just 4 kilobyte pages at a time. And we discussed in the last lecture what happens when frames run out. When we cut run out of memory, well, we pick
7:38 pages to evict to disk. Or we could just run out of memory killer and just randomly kill a process and free all of its physical memory. But real programs, have you ever asked for a page before this course? Probably not. You probably asked for like a 100 bytes at a time or some other number, not pages. So something has to cut the pages into smaller chunks and or pieces and take the pieces back once you're done with them. That's maloc and free. And today is how we actually would implement it. So this would be in the C library, not the kernel. But you could imagine the kernel needs to use some memory itself. And everything would also apply to the kernel as well if it was to
8:26 ask uh to use memory as well because if you're in the kernel you don't have maloc and free because well you don't have the C standard library. So simplest allocate or simplest strategy
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.