Allocators, fragmentation and OOM
medium 7 minAn allocator subdivides pages into objects and retains free space for reuse.
Video lesson · Chapter 1 of 8
Live objects fall but RSS stays high
Theory
Read or listen first. About 2 min.
What it is
An allocator subdivides pages into objects and retains free space for reuse. An application can release most objects while one live allocation pins each of many pages. The heap then contains substantial free space but has few whole pages available to return. Virtual reservation, committed memory, resident memory and live object size answer different questions.
Why it exists
A batch worker doubles its resident footprint after a change to allocation sizes. Compare a live-object profile just after equivalent batches, allocator statistics and cgroup anonymous memory. A stable live heap alongside growing slack suggests fragmentation or cached arenas. Raising the memory limit buys time but does not distinguish retention from allocator behavior.
A growing reachable object graph after equivalent batches calls for object lifetime analysis before allocator tuning.
Live objects fall but RSS stays high
Inside this chapter
- Stack vs. Heap memory mechanics
- A stack frame usually lives for a call’s duration. Heap objects have independent lifetimes governed by an allocator or garbage collector. Lifetime and ownership determine retention.
- Memory Fragmentation (Internal vs. External)
- Internal fragmentation wastes space within allocated units. External fragmentation leaves enough total free memory in pieces too small or poorly placed for the required allocation.
- Memory Compaction
- Compaction moves movable pages to create larger contiguous ranges. Allocation can therefore incur latency even when the total free page count looks adequate.
- Overcommit Accounting
- Overcommit permits promises of virtual memory beyond immediately available backing under some policies. First touch and later memory pressure can expose costs absent at reservation time.
- The OOM (Out of Memory) Killer selection algorithm
- An allocation that cannot make progress may trigger an OOM decision at host or group scope. Check the termination record and controller events before attributing a restart to a leak.
- Slab Allocator (kmalloc internals)
- Kernel slab caches reuse storage for frequently allocated object types. Slab growth can consume substantial memory outside an application heap, so inspect the responsible object class.
- Userspace allocators: jemalloc and tcmalloc
- Userspace allocators use size classes, arenas and thread caches to reduce contention. Retention policy influences resident memory after load falls even when application objects are freed.
- Memory Pools and Arena Allocation
- A pool reuses preallocated storage. A per-request arena can release many objects together, but a single long-lived reference can extend the lifetime of all allocations in that arena.
How it works
Step through the idea, or press Play.
Allocate from a region. A contiguous free region supplies objects of several sizes and lifetimes.
Understand it
Worked incident
Live objects fall but RSS stays high
| Observation | Resident memory after batch (MiB) | Interpretation |
|---|---|---|
| Baseline | 180 | Similar objects share pages and most empty pages can return. |
| Incident | 410 | Mixed lifetimes leave a few survivors across many pages. |
| After intervention | 205 | Group similar lifetimes or bound arenas. Check retained memory and allocation latency over several batches. |
Choose the next investigation
Summary
The whole topic in one minute.
Reachable objects explain live bytes. Arenas and resident pages can remain large after the program has freed the objects.
A growing reachable object graph after equivalent batches calls for object lifetime analysis before allocator tuning.