Operating Systems Reference/Memory Allocation

Heap Allocation & malloc

How malloc/free interact with the kernel (brk/mmap), allocator strategies (bins, arenas), fragmentation, and why long-running servers care about allocator choice (glibc vs jemalloc vs tcmalloc).

3/5Overview: 25m

Two paths to the kernel

User-space allocators (malloc/free) sit above two kernel mechanisms:

MechanismSyscallTypical use
Program breakbrk/sbrkSmall, contiguous heap growth
Anonymous mappingmmap(MAP_ANONYMOUS)Large allocations, allocator arenas

glibc malloc uses arenas (per-thread to reduce lock contention), fastbins for small freed chunks, and an mmap threshold (~128 KiB default) above which allocations go directly to mmap — returning them on free via munmap.

Allocator internals (conceptual)

  • Splitting — a free 256-byte chunk satisfies a 64-byte request; remainder stays on free list.
  • Coalescing — adjacent free chunks merge to fight external fragmentation.
  • Bins — size-class free lists for O(1) reuse.
  • Internal fragmentation — rounding up to alignment or size class wastes bytes inside allocated blocks.

Allocator choice in production

AllocatorStrengthCommon deployment
glibc mallocDefault, good enough for mostEverything unless measured otherwise
jemallocLower fragmentation, arena tuningFirefox, Redis, some JVM alternatives
tcmallocFast per-thread cachesgRPC, high-QPS C++ services

Switch allocators only with profiling evidence (malloc_info, heap profilers, RSS over 24h soak).

Common bugs

  • Memory leak — allocated, never freed; RSS climbs monotonically.
  • Use-after-free — dangling pointer; security and heisenbugs.
  • Double free — corrupts allocator metadata; often exploitable.
  • Heap overflow — write past allocation; corrupts adjacent chunk headers.

Senior-level signal

Long-running servers that "slowly eat RAM" often have fragmentation, not leaks — RSS high but malloc_info shows plenty of free heap. MALLOC_ARENA_MAX=2 is a blunt glibc knob for container memory limits; jemalloc's background_thread helps return pages to OS.

Where this goes next

mmap & Shared Memory covers the other major allocation path — mapping files and sharing pages across processes for IPC and databases.

Further Reading

Hands-On Tasks (Optional)

Low-setup exercises on your local machine. No autograding — the goal is to build intuition, not pass a test.

  • Observe heap usage of a process

    On Linux with glibc: write a tiny loop that malloc's 1MB repeatedly and watch `ps -o rss,vsz -p <pid>` grow. Stop before OOM. Alternatively inspect `/proc/<pid>/smaps_rollup` for anonymous mapping size.

    20m